Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable disable anchor, Jquery

Tags:

jquery

click

Here is the html:

<ul>
    <li><a href="http://xyz.com"><img src="./xyz.png"/></a></li>
    <li><a href="http://xyz.com"><img src="./xyz.png"/></a></li>
    <li><a href="http://xyz.com"><img src="./xyz.png"/></a></li>
    <li><a href="http://xyz.com"><img src="./xyz.png"/></a></li>

</ul>

If I click the second li it should go to the link but not if I click any other li. I have this code, I want to know a way to enable the default action.

 $(li[k]).click(function(){//increase the height});
 $(li[k]).find('a').click(function(e) {
    e.preventDefault();
    });
like image 667
Paneri Avatar asked Feb 26 '26 00:02

Paneri


2 Answers

I'd suggest something like the following:

 $(li[k]).click(function(){//increase the height});
 $(li[k]).find('a').click(function(e) {
        if ($(this).parent().index() != 1){
            e.preventDefault();
        }
    });

The way that this works is that if the index() of the clicked li element (based on its position among its siblings) is not equal to 1 (JavaScript arrays being zero-based, 1 is the second element in the array), the e.preventDefault() fires; otherwise (if the index() is equal to 1) the default action is permitted.

like image 97
David Thomas Avatar answered Feb 27 '26 14:02

David Thomas


If I understand correctly, you want to re-enable the default behavior of one of the links?

You can do that like this:

$(li[k]).find('a').unbind("click");

This will remove the click handler from the selected element, essentially returning it to its default behavior.

like image 44
Kyle Trauberman Avatar answered Feb 27 '26 15:02

Kyle Trauberman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!