alert($(this).children().next().html());
returns
<a href="thelink.php?id=XXXX">click me...</a>
How do I change above statement to grab only the href?
Have tried attr('href') but can't get it to work.
(Yes, jq-noob and no time.)
//edit - code added:
<div class="row">
<div class="date">
<h2>27</h2>
<h3>dec</h3>
</div>
<h2><a href="/wpb/index.php?id=4192#4199">the title..</a></h2>
<p>the description...</p>
</div>
jq invoked by:
$("#threecol .row").click(function() {
alert($(this).children().next().href);
});
regards,
jQuery children() MethodThe children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.
What method can be used to retrieve the href attribute of an element? Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.
If you want to get any element that has part of a URL in their href attribute you could use: $( 'a[href*="google.com"]' ); This will select all elements with a href that contains google.com, for example: http://google.com.
Find the attribute value using .attr('href').
EDIT: The first anchor within a H2:
alert($(this).find('h2 a:first').attr('href'));
You could also reference it via a class if you prefer and use the class in your anchor.
alert($(this).find('.myLink').attr('href'));
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