Travis Hedrick
Travis Hedrick

Reputation: 41

Clickable Jquery Div returns "undefined" if you click directly on text

Thanks in advance for any help. I am using the following code to make an entire li "Clickable". The problem is, if you click on any of the actual text other than the link itself (red, blue & green stats), it returns "undefined". See live link here: http://174.122.31.60/.

$(".available-properties li").click(function(){
 window.location=$(this).find("a").attr("href");
 return false;

});

Upvotes: 1

Views: 946

Answers (4)

thinice
thinice

Reputation: 710

You'll get a race condition if you don't filter out the 'a':

    $('.available-properties li').click(
        function(e) {
            if(!($(e.target).is('a'))) {
                window.location = $(this).children("a:first").attr("href");
            }
        }
    );

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35983

Try this code:

$(".available-properties li").click(function(){
 window.location=$(this).parent().parent().find("a").attr("href");
 return false;
});

EXAMPLE

Upvotes: 1

j08691
j08691

Reputation: 208040

Just change this:

$(".available-properties li").click

to this:

$(".available-properties > li").click

This way you only select the child LI of the available-properties UL element.

Upvotes: 0

Bart Groenhuizen
Bart Groenhuizen

Reputation: 296

This will work. With > the click function will work only on the parent and not the li in the child ul.

$(".available-properties > li").click(function(){
    window.location=$(this).find("a").attr("href");
    return false;
});​

Upvotes: 0

Related Questions