How to find the id of the first anchor html element within the first element of an unordered list using JQuery

No Comments

With getting the id I mean getting the attribute id’s value for the first element within the first element of the unordered list tag.

 

Answer:

We can obtain this by using the :first Selector from JQuery. Read more about the selector here

Example:

HTML 

<ul class="custom_class">
    <li><a href="test1.html" id="1" name="link1" /></li>
    <li><a href="test2.html" id="2" name="link2" /></li>
    <li><a href="test3.html" id="3" name="link3" /></li>
    <li><a href="test4.html" id="4" name="link4" /></li>
    <li><a href="test5.html" id="5" name="link5" /></li>
</ul>

JQuery

        var id = $('.custom_class li a:first').attr('id');

Image Credits: Photo by Isis França on Unsplash.

Last Updated On March 21, 2018

Related Articles

No results found

Leave a Reply

avatar
  Subscribe  
Notify of
Menu
Copy link