All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Get Element By Name

Last Updated : Mar 11, 2024

jQuery Get Element By Name

In this article we will show you the solution of jQuery get element by name, this selector can be helpful for locating elements in web pages generated by server-side frameworks which generate HTML with standardised element IDs.

Use classes to group related elements if you can, as doing so will be faster than employing a class selector. Now move to the concept of jQuery get element by name.

Step By Step Guide On jQuery Get Element By Name :-

All components matched by "input[name=myname]" that possess a name value exactly equivalent to "myname" are chosen by the expression "$("input[name=myname]")".

Syntax :-

$("element[attribute-name=attribute-value]")

Parameters :-

Here is a list of each of the parameters that this selection uses, along with a brief description of each.

  • element - Any common HTML tag name, such as div, p, em, image, li, etc., is considered an element.
  • attribute-name - The element above has an attribute called attribute-name.
  • attribute-value - Assigning a value to the attribute on the following element is called attribute-value.
  • Returns - This selector also delivers an array containing the elements it has located, just like every other jQuery selector does.

How can I use jQuery to choose an element by name?

There are two ways to choose an element based on the name attribute:

  • Utilizing the name selector technique
  • We may learn about both approaches through using JavaScript to fetch the element via name and then giving it to jQuery.

Method 1 - by using name selector technique

You can choose an element by its name by using the name attribute selector.

The components that have a value absolutely equal to the supplied value are selected by this selector.

Syntax :-

[name="nameOfElement"]

Method 2 - The second approach involves using JavaScript to retrieve the element via name and passing it to jQuery.

To choose the necessary element, use the JavaScript getElementsByName() method.

The selected element can then be provided to a jQuery function for use as a jQuery object.

Syntax :-

selector = document.getElementsByName('nameOfElement');
element = $(selector);
<!DOCTYPE html>
<html>
<head>
 <title>
 Jquery get element by name
 </title>
</head>
<body>
 <center>
  <h1 style="color: green">Welcome to Talkerscode</h1>
  <b>Jquery get element by name</b>
  <p>
   The textbox below has the <b>name attribute 'address'.</b>
  <form>
   <textarea rows="1" cols="10" 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>
  <script src=
"https://code.jquery.com/jquery-3.3.1.min.js">
  </script>
  <script type="text/javascript">
   function selectByName() {
    element = $('[name="area"]');
    element.hide();
   }
  </script>
 </center>
</body>
</html>
  1. HTML & HEAD These are the first tags in our code.
  2. Following that, we give our page a title.
  3. Next, we begin the body section of the code.
  4. We align to the middle.
  5. Using the H1 tag, we then provide a heading.
  6. Following that, we validate certain statements utilising a form's textarea.
  7. Next, we design the button.
  8. Next, the jQuery library is imported.
  9. We then begin our script.
  10. Next, we develop the selectByName method.
  11. The element is then called by name.
  12. The element is then hidden using the hide() function.
  13. Finally, we use SCRIPT, BODY, and HTML elements to finish our code.

Conclusion :-

Consequently, we have effectively acquired knowledge of the jQuery get element given name idea.

Using classes to group related components will be quicker than using class selectors, as can be shown in the statement Use class for group related parts if you can.

I hope this article on jQuery get element by name helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪