How to Get the ID of an Element using jQuery
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
Use the jQuery
attr()
MethodYou can simply use the jQuery
attr()
method to get or set the ID attribute value of an element.The following example will display the ID of the DIV element in an alert box on button click.
You can also get the ID of multiple elements having same class through loop, like this:
You can also get the ID of individual element when using class selector based on index starting from 0, for example, to get the ID of first element in a set of matched elements you can use $(".box").get(0).id or $(".box")[0].id.
Similarly, to get the ID of last element you can use something like this, $(".box").get($(".box").length - 1).id or $(".box")[$(".box").length - 1].id, because jQuery selector returns a collection of matched elements not a single element.
need an explanation for this answer? contact us directly to get an explanation for this answer