I have the following list
<ul class="tabs">
<li><a href="testlink.php">First link</a></li>
<li><a href="testlink2.php">Second link</a></li>
</ul>
I would like to grab the href for the first link. This link needs to be in a var link so I can use it to dynamically retrieve a page content. Thanks for any help :)
You can use the :first selector:
var url = $("ul.tabs a:first").attr("href");
Or the :eq() selector:
var url = $("ul.tabs a:eq(0)").attr("href");
$(function() {
var href = $('ul.tabs').find('a').slice(0,1).attr('href');
});
This is pretty fast, if you're after performance try to avoid Sizzle selectors:
http://jsperf.com/selector-sizzle-vs-methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With