views:

35

answers:

3

I have a LI element whith a link, and other links inside.

It would be something like this:

jQuery:

$('li').click(function() {
   window.location = $(this).children('a.li-url').attr('href');
});

HTML:

<ul>
    <li>
        <a href="http://www.link.com" class="li-url"></a>
        <h1>Title</h1>
        Lorem ipsum dolor <a href="http://www.something.com"&gt;Something&lt;/a&gt;. Etc, blah blah.
    </li>
</ul>​

The problem is that when I click on "something", It also loads www.link.com (you can see this if you alt+click the something link. It will load something.com in a new tab while loading link.com in your current tab.)

I need to ONLY load something.com when I click on "something"..

Thanks!

A: 

try this.preventDefault();

gov
THANK YOU! That works perfect. One last thing, it would be possible to make possible to load the LI link in a new tab by alt+click, like a normal link?
Santiago
Sorry, that preventDefault also broke the LI link... So, doesn't works just fine.
Santiago
That's not what preventDefault() will do.
Strelok
sorry , i mean stopPropagation() , but typed preventDefault(). anyway you got the answer from others.
gov
+1  A: 
$('li').click(function(e) {
    if (e.target.tagName.toLowerCase() == 'li') {
        window.location = $(this).children('a.li-url').attr('href');
    }
});​

crazy demo


Welcome to stackoverflow.com
Don't forget to accept an answer

Reigel
Thanks! That works. Do you know if it's possible to make possible to load the LI link in a new tab by alt+click, like a normal link?
Santiago
+1  A: 

Add this bit of JS.

$('li a:not(.li-url)').click(function() {
    event.stopPropagation();
});
​

Using my selector or something else to target the other link within the li.

Strelok