views:

56

answers:

5

I have alot of:

<li><a href="google.com">LINKY</a></li>

type links on my site and much prefere the whole Li to be clickable instead of just the text. So was thinking of applying some nice jquery that will hunt any $('li a') down and make it so when you click the <li> it will trigger the click on a <a>

        $('li a').each(function(index) {
            $(this).parent().bind('click', function() {
                $(this).children().click();
            });
        });

This fails to work but im not sure why? any ideas?

+1  A: 

Calling click() on a link doesn't perform the default action (going to the address). You could manually make a click handler:

$('li a').each(function(index) {
            $(this).parent().bind('click', function() {
                window.location = $(this).children().attr('href');
            });
        });
Matthew Flaschen
This will break expected behavior if you shift/ctrl click on a link...
gnarf
@gnarf, that's true. Can you suggest a way to better mimic the default browser behavior?
Matthew Flaschen
@Matthew - Check the `event.target.nodeName != 'A'` and it'll help on the dual open from a ctrl+click, but still not perfect: http://jsfiddle.net/nick_craver/FzaVS/
Nick Craver
+2  A: 

It is much simpler if you just bind the click event to the <li> element.

Event bubbling will ensure that it is triggered when you click the <a> as well.

Here's an example: http://jsfiddle.net/MB9Fm/

$('li:has( > a)').click(function() {
     alert('I was clicked');
     return false;
});

EDIT:

I may have misunderstood the intention for the click handler. If all you wanted to do was visit the href location, I'd agree with the CSS solutions of possible. Otherwise, using js, do something like this:

http://jsfiddle.net/nkGga/

$('li:has( > a)').click(function() {
     window.location = $(this).children('a').attr('href');
});

The way you were calling .click() in your comment would cause an infinite loop, an I'm not sure that it would actually take you to the href location anyway.

patrick dw
+4  A: 

Wouldn't it be better to stretch the anchor to the size of LI?

li > a {
   display: block;
   width: 100%;
   /* optionally height: some height */
}
Rafael
You may need to account for browsers that don't deal with child selectors (and "li a" works fine, maybe with a class somewhere in there), but otherwise this is much more robust than JavaScript. +1
chryss
would be a brilliant fix but the block messys up horizantial aligned li's!
Steve
Well, float them, or something! There are tons of ways of getting them where you want.
chryss
A: 

Look into the .delegate() functionality added in 1.4.2, this is more suited to your desires.

$("li").delegate("a", "click", function(event){
  //do what you want here
  return false; //prevent default action and prevent bubbling up
});
Mark Schultheiss
note: remove the "return false;" if you DO want default action and bubbling up.
Mark Schultheiss
The OP wants to make the `<li>` clickable. Using `.delegate()` in this manner (even without the `return false`) will not trigger the handler when you click the `li`. Only when you click the `<a>`.
patrick dw
A: 

Try this:

$('li:has(a)').each(function() {
    $(this).bind('click', function(index){
        $(this).find('a').trigger('click');
    });
});
You can't call `.live()` like this, it needs a selector :)
Nick Craver
Added bind instead. Cheers!