jQuery

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

Written by Ian Carnaghan · 29 sec read >

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

Last Updated On March 21, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. Profile

Leave a Reply

avatar
  Subscribe  
Notify of