Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get href in children()

Tags:

jquery

  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,

like image 337
Teson Avatar asked Mar 14 '11 13:03

Teson


People also ask

How do you get children of children in jQuery?

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?

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.

How do I select an element using href?

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.


1 Answers

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'));
like image 61
Andrew Avatar answered Oct 12 '22 11:10

Andrew