How to select an element by name with jQuery ?
Last Updated :
17 Sep, 2024
Selecting an element by name with jQuery allows you to target HTML elements using their `name` attribute. This method is commonly used in form handling, where input fields, checkboxes, or radio buttons share the same name, enabling you to easily manipulate or retrieve their values.
We will understand both methods with the help of examples.
Using the name selector Method
The name selector method in jQuery uses the [name="nameOfElement"] syntax to select elements based on their name attribute. This method precisely targets elements with matching names, making it ideal for form elements like inputs and radio buttons that share a common name.
Syntax:
[name="nameOfElement"]
Example: In this example we use jQuery to select an element by its name attribute. When the button is clicked, the selectByName() function hides the input with the name="area".
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to select an element by name with jQuery?
</title>
</head>
<body>
<center>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to select an element by name with jQuery?</b>
<p>
The textbox below has the <b>name attribute 'address'.</b>
<form>
<textarea rows="4"
cols="40"
name="address">
</textarea>
</form>
</p>
<p>
The textbox below has the
<b>name attribute 'area'.</b>
<form>
<input type="text" name="area">
</form>
</p>
<p>Click on the button to hide the input with
the name 'area'</p>
<button onclick="selectByName()">
Click to hide the area input
</button>
</center>
<script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script type="text/javascript">
function selectByName() {
element = $('[name="area"]');
// Hide the element
element.hide();
}
</script>
</body>
</html>
Output:
name selector MethodUsing JavaScript getElementsByName() Method with jQuery
The JavaScript getElementsByName() method selects elements by their name attribute and returns a NodeList. This list can be passed to jQuery using $() to convert it into a jQuery object, allowing further manipulation with jQuery methods like hide() or show().
Syntax:
selector = document.getElementsByName('nameOfElement');
element = $(selector);
Example: In this example we use JavaScript's getElementsByName() method with jQuery to select the textarea with the name="address". When the button is clicked, the selectByName() function hides the selected element.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to select an element by name with jQuery?
</title>
</head>
<body>
<center>
<h1 style="color: green">GeeksforGeeks</h1>
<b>How to select an element by name with jQuery?</b>
<p>
The textbox below has the
<b>name attribute 'address'.</b>
<form>
<textarea rows="4" cols="40" name="address"></textarea>
</form>
</p>
<p>
The textbox below has the
<b>name attribute 'area'.</b>
<form>
<input type="text" name="area">
</form>
</p>
<p>
Click on the button to hide the
input with the name 'address'
</p>
<button onclick="selectByName()">
Click to hide the address input
</button>
<script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script type="text/javascript">
function selectByName() {
selector = document.getElementsByName('address');
element = $(selector);
// hide the element
element.hide();
}
</script>
</center>
</body>
</html>
Output:
Getting the element by their namejQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous for its philosophy of “Write less, do more". Please refer to the jQuery Tutorial and jQuery Examples article for further details.
Similar Reads
How to select element by ID in jQuery ? In this article, we are going to see how to select the element by its id using jQuery. To learn about this topic one should have prior knowledge about HTML, CSS, JavaScript and jQuery. Using JavaScript: This question is also solved by a popular JavaScript method called document.getElementById() whic
2 min read
How to select multiple elements using jQuery ? In this article, we will learn how to select multiple elements using JQuery. JQuery is the fastest and most lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. JQuery is widely famou
2 min read
How to select elements by attribute in jQuery ? jQuery is a lightweight JavaScript library. In the vanilla JavaScript language, the getElementById method is used to select an element. However, jQuery provides a much lighter alternative for the same purpose. The 'jQuery Selector' allows the user to manipulate HTML elements and the data inside it(D
2 min read
How to find an element by text using jQuery ? We will learn how to find an element using jQuery's API. This article requires some knowledge of HTML, CSS, JavaScript, Bootstrap, and jQuery. The elements can be selected based on whether they contain the string we want to find. This can be done using the jQuery contains selector to select elements
2 min read
How to select elements with no visible children using jQuery ? In this article we are going to learn how to select elements whose property is not visible or hidden. It simply means that the display property of that particular element is hidden, and we need to show whatever present in that element using the Jquery.We can easily do this by using the Jquery:hidden
2 min read