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

H

How Can We Help?

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

About the author

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.

About Author

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.

Follow Me