Jump to content

Using DOM to change the onClick command


george
 Share

Recommended Posts

How can I change the event handler using DOMHere is one of the many wrong ways I have tried to do this.

document.getElementById('expAll').event.onclick="expandAll(false)";

expAll is the ID of an anchor tag.expandAll(false) is a javascript function. The entire code can be seen at My WebsiteI may be able to switch the event handeler using just the javascript function itself. But I do not know how to create a variable in Javascript that will switch and retain the value of a logical variable.

Link to comment
Share on other sites

First and foremost, apply the callback on the onclick object. There's no "event" object in a DOM element*. Second, specify a reference to the function. Example:

document.getElementById('expAll').onclick=expandAll;

Specifying expandAll(false) will mean that the result of the function will become the event handler. Unless expandAll(false) returns a reference to a function, that's probably not what you want.* The callback function however receives one argument that is an event object. It contains information about the event such as the (in this case) clicked element, the button(s) pressed, etc.

Link to comment
Share on other sites

First and foremost, apply the callback on the onclick object. There's no "event" object in a DOM element*. Second, specify a reference to the function. Example:
document.getElementById('expAll').onclick=expandAll;

This simulates the onclick event. I want to reset what happens when the element is clicked. I end up in a continuous loop.
function expandAll(bValue) {	 Alert(bValue);	 if bValue {		document.getElementById('expAll').onclick=expandAll(false);	 } else {		document.getElementById('expAll').onclick=expandAll(true);	 }

I want to reset what happens when the element is actually clicked.

Link to comment
Share on other sites

tryfunction expandAll(bValue) { alert(bValue); if (bValue == true) { document.getElementById('expAll').setAttribute('onclick', 'expandAll(false)'); document.getElementById("expAll").onclick = function(){expandAll(false);};//IE document.getElementById('expAll').innerHTML="false"; } else { document.getElementById('expAll').setAttribute('onclick', 'expandAll(true)'); document.getElementById("expAll").onclick = function(){expandAll(true);};//IE document.getElementById('expAll').innerHTML="true"; } }<a href="java script:void(0);" id="expAll" onclick="expandAll(false);">false</a>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...