2

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);


}
4
  • 3
    this.getAttribute('id') or this.id Commented May 22, 2015 at 14:01
  • Read a basic tutorial on the DOM API. You'll find useful routines there such as getAttribute. StackOverflow tends to function as a poor substitute for just reading the documentation. Commented May 22, 2015 at 14:01
  • 2
    The real answer here is addEventListener, that way you'd solve the issue with the inline handler and get access to this Commented May 22, 2015 at 14:02
  • Specifically, you could consult developer.mozilla.org/en-US/docs/Web/API/Element/id. This was the first result that popped up when I googled "html element id". Commented May 22, 2015 at 14:07

3 Answers 3

5

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>

Sign up to request clarification or add additional context in comments.

Comments

1

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);
 }

Comments

1

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.