get Id of input field using JavaScript

21,447

Solution 1

You can simply get it using .id attribute like this:

this.id;

Or you can also use .getAttribute() method:

this.getAttribute('id')

So your example should be, like this:

 function load_getUserListByGroupID(element) {
   alert(element.id);
 }
<a href="#" class="SearchUser_Icon Hyperlink_Text" onclick="load_getUserListByGroupID(this)" id="2">aa</a>

Solution 2

Pass this to onclick event handler. Then you can directly get its id.

Use

<a href="#" class="SearchUser_Icon Hyperlink_Text" onclick="load_getUserListByGroupID(this)" id="2"></a>

Script

 function load_getUserListByGroupID(elem){
    alert(elem.id);
 }

Solution 3

Use a variable,

onClick="reply_click(this.id)"

function reply_click(clicked_id)
{
    alert(clicked_id);
}

Add the OnClick function the element you'd like, this will also throw the ID value when processing to the function.

Hope this helps :)

Share:
21,447
K.Z
Author by

K.Z

Updated on November 18, 2021

Comments

  • K.Z
    K.Z over 2 years

    I have input in grid, which each unique ID, output as following;

    <a href="#" class="SearchUser_Icon Hyperlink_Text" onclick="load_getUserListByGroupID()" id="2"></a>
    

    I want to know what is equivalent to $(this).attr("ID") in javaScript

     function load_getUserListByGroupID()
    {
    
        var selectedGroupID = document.getElementById('input');
    
        alert(selectedGroupID);
    
    
    }