How to select an element by name with jQuery?

An element can be selected by the name attribute using 2 methods:

Method 1: Using the name selector method

The name attribute selector can be used to select an element by its name. This selector selects elements that have the value exactly equal to the specified value.



Syntax:

[name="nameOfElement"]

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>
        <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>
    </center>
</body>
  
</html>

chevron_right


Output:

  • Before clicking the button:
    selector-before
  • After clicking the button:
    selector-after

Method 2: Using JavaScript to get the element by name and passing it on to jQuery

The JavaScript method getElementsByName() can be used to select the required element and this can be passed to a jQuery function to use it further as a jQuery object.

Syntax:

selector = document.getElementsByName('nameOfElement');
element = $(selector);

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking the button:
    normal-js-before
  • After clicking the button:
    normal-js-after


My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.