Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save and restore "onclick" action on jQuery objects

I want to disable a whole bunch of objects on the page, and then re-enable them later. Since some of them are tags rather than buttons, I disable them by removing their onclick attr. I've tried to store the old handler in a .data(), but unfortunately when I attempt to restore them with $(obj).attr('onclick',$(obj).data('onclick')), it calls the function rather than restoring it to the attribute. And if I try to store it in a different attribute instead of a data, it doesn't store the function, it stores the return value for the function.

Is there any way to accomplish this without re-writing every tag and every onclick handler on my page?

if( doEnable) {

    $(obj).attr('href', $(obj).data('href'));
    $(obj).attr('onclick', $(obj).data('onclick'));


    $(obj).removeClass(EIS.config.classes.disabled);
    $(obj).show();

}
else {
    // Save the things you're going to remove
    $(obj).data('onclick', $(obj).attr('onclick'));
    $(obj).data('href', $(obj).attr('href'));

    $(obj).prop("href", null);
    $(obj).prop("onclick", null);

    $(obj).addClass(EIS.config.classes.disabled);
    $(obj).show();
   }

By the way, this code seems to work fine in Chrome and Firefox, but only sometimes in IE8 and never in IE6. Unfortunately the client tests first in IE6.

like image 668
Paul Tomblin Avatar asked Jul 11 '11 22:07

Paul Tomblin


People also ask

How to trigger the onclick function in jQuery?

One of the fast, small, and feature-rich libraries in JavaScript is jQuery. jQuery is used for making HTML documents, event handling, traversal, and manipulation, etc. Whenever an HTML element is clicked, there is the occurrence of the jQuery click event. To trigger the onclick function in jQuery, click () method is used.

What are onclick and onLoad events in JavaScript?

There are a number of DOM (Document Object Model) events that you can listen for in JavaScript, but onclick and onload are among the most common. The onclick event in JavaScript lets you execute a function when an element is clicked.

What is the use of jQuery click event?

Click () function is an in-built function in jQuery that starts a click event. Whenever a relevant element is clicked upon, an event is fired by a click () method. This event then fires a click event of elements higher up in the document tree (or event chain).

How do you click a button in jQuery?

The jQuery is enclosed within the <script> tags. The method click ()is attached to the button’s id and an argument is taken as a call back function which is an anonymous function. The string “Button clicked” is returned by the callback function.


1 Answers

$(obj).attr('onclick', ...

is ambiguous, has results that differ in different versions of jQuery and different browsers. It probably doesn't do what you want. You should avoid using attr on event handlers.

The problem is the disconnect between the onclick attribute and the onclick property. jQuery has tried to brush the difference between an attribute and a property under the carpet in the past, using attr to access both, but they're quite different. This was changed in jQuery 1.6, and partially reverted in 1.6.1, to widespread controversy, confusion and incompatibility.

For many properties, the values of an attribute and the corresponding DOM property are the same; for others, including all properties that aren't strings, they aren't. Event handlers certainly aren't: the property is a Function object, whereas the string attribute might be (a) the original string of the onclick="..." attribute in the HTML, (b) nothing (if the onclick was assigned from script to be a Function object) or (c) unavailable (in older IE).

To access the event handler Function property, use prop() in jQuery 1.6:

$(obj).data('onclick', $(obj).prop('onclick'));
...
$(obj).prop('onclick', $(obj).data('onclick'));

or just use plain old JavaScript which is actually simpler and more readable; jQuery wins you nothing here.

obj._onclick= obj.onclick;
...
obj.onclick= obj._onclick;

Either way this is not going to reliably ‘disable’ elements since they can (and very likely will, if you're using jQuery) have other event listeners registered on them, using addEventListener/attachEvent rather than the old-school event handler interfaces.

like image 89
bobince Avatar answered Sep 18 '22 08:09

bobince