views:

77

answers:

3

I'm feeling so dumb asking this question, but I can't figure out a clean way to write this...

Heres the HTML

<li>
<a class='link_parent' href='#'>Test</a>
</li>

I want the click function of the parent LI to redirect the href of the a with .link_parent...

so...

$('a.link_parent').each(function() {
  $(this).parent().click(function() {
    // Need to access $(this).attr('href') of the above object
    // but can't since $(this) is now the <li> object
    // help!
  });
});
+1  A: 
$("li").click(function(){
  $("a.link_parent", this).trigger("click");
});

Just a short explanation of scope. this is not the only way to get access to items:

$("a.link_parent").each(function(i,o){
  // 'o' will always be a reference to this anchor
  $(this).parent().click(function(e){
    // 'e.currentTarget' will be a reference to this parent
    alert(o + " " + e.currentTarget);
  });
});
Jonathan Sampson
+1  A: 
$('a.link_parent').each(function() {
    var link = this;
    $(this).parent().click(function() {
        link.attr('href');
    });
});
prodigitalson
A: 

You can do it the other way round.Dont know the exact JQuery way of writing it.

Add a click event to the "li" element and grab its first child i.e. the "a: element and get its href.

Zaje