3

I want to disable all click events on my page but some of events are still getting called, suppose I have html as below

<div id='parent'>
    <table>
        <tr>
            <td><input type = 'text'/></td>
            <td><select><option>A</option><option>B</option></select></td>
            <td><a href='#' onclick="alert('Called')">Click </a></td>
        </tr>
        <tr>
            <td>dscsdcd</td>
            <td>dscsdcd</td>
            <td>dscdscdsc</td>
        </tr>
        <tr>
            <td>sdcdsc</td>
            <td>dssdcdsc</td>
            <td><input type='submit' value='Button'/></td>
        </tr>
    </table>
</div>

And script as below

$('#parent').click( function(e)
{
     e.stopPropagation();
     e.preventDefault();
     e.stopImmediatePropagation();
     return false;
});

when I click anchor tag, it alerts Called which it shouldn't because I am disabling event for it's parent. Looks like anchor tag is overriding onclick, if it is then how to prevent it? if not, then what could be the solution?

JS Fiddle

5
  • 1
    it should be $('#parent a').... Commented Oct 27, 2015 at 13:28
  • 1
    events propagate UP from a to the parent. so events tidied to a will fire first. Commented Oct 27, 2015 at 13:28
  • @ThinkDifferent but if it was other element then? basically I want to disable it for any element under that div. Commented Oct 27, 2015 at 13:29
  • @BG101 thanks for information, but can I disable that behavior? because this scenario is quit practicle. Commented Oct 27, 2015 at 13:30
  • Just added a plugin below which allows you to disabled (remove) and enable (re-add) click events for both onclick and jquery click. Commented Oct 27, 2015 at 15:19

5 Answers 5

5

Just remove the onclick attribute from elements that have it, and unbind the 'click' event using .off('click') method

$('#parent *[onclick]').removeAttr('onclick').off('click');
Sign up to request clarification or add additional context in comments.

4 Comments

if I want to enable click again, can I do it programmatically?
This is removing, not disabling.
@Imad, you can just bind it again using .on('click'...)
@BG101, check it before commenting, the .removeAttr() removes the attribute, and then the .off('click') unbinds the click event.
1

onclick attribute has a higher priority here. You can loop your <a> elements and unset this attribute:

$('#parent a').each(function () {
  $(this).attr('onclick', '');
}).click(function () {
  // ...
});

2 Comments

if I want to enable click again, can I do it programmatically?
Store $(this).attr('onclick') value and assign it back.
1

try this plugin to temporary disable onclick AND jQuery click events:-

$.fn.disableClick = function (disable){
    this.each(function() {
        if(disable){
            if(this.onclick)
                $(this).data('onclick', this.onclick).removeAttr('onclick');
            if($._data(this, 'events') && $._data(this, 'events').click)
                $(this).data('click', $.extend(true, {}, $._data(this, 'events').click)).off('click');
        }
        else{
            if($(this).data('onclick'))
                this.onclick = $(this).data('onclick');
            if($(this).data('click'))
                for(var i in $(this).data('click'))
                    $(this).on('click', $(this).data('click')[i].handler);
        }
    });
    return this;
};


//disable clicks
$('#parent *').disableClick(true);

//enable clicks
$('#parent *').disableClick(false);

DEMO

Comments

0

If you want to disable it for any element under that div you must type:

$('#parent *').click( function(e)
{
     e.stopPropagation();
     e.preventDefault();
     e.stopImmediatePropagation();
     return false;
});

3 Comments

this is not guaranteed, as it depends what order events are bound to an element
@BG101 means it is not possible to prevent child clicks through parent?
No, you can prevent child clicks through parent. @BG101 say in this case you must be carefully with order of events are bound to an element.
0

There is an attribute you should use to make all things disabled.

var nodes = document.getElementById("parent").getElementsByTagName('*');
for(var i = 0; i < nodes.length; i++) {
     nodes[i].disabled = true;
}

Unfortunately disable is not valid with anchor tags.

There you should use :

var nodes = document.getElementById("parent").getElementsByTagName('a');
for(var i = 0; i < nodes.length; i++) {
  nodes[i].onclick = function(e){
    e.preventDefault();
    // YOUR CODES HERE
  }
}

try to combine these two methods according to your needs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.