Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an onclick property set with setAttribute fail to work in IE?

Ran into this problem today, posting in case someone else has the same issue.

var execBtn = document.createElement('input'); execBtn.setAttribute("type", "button"); execBtn.setAttribute("id", "execBtn"); execBtn.setAttribute("value", "Execute"); execBtn.setAttribute("onclick", "runCommand();"); 

Turns out to get IE to run an onclick on a dynamically generated element, we can't use setAttribute. Instead, we need to set the onclick property on the object with an anonymous function wrapping the code we want to run.

execBtn.onclick = function() { runCommand() }; 

BAD IDEAS:

You can do

execBtn.setAttribute("onclick", function() { runCommand() }); 

but it will break in IE in non-standards mode according to @scunliffe.

You can't do this at all

execBtn.setAttribute("onclick", runCommand() );  

because it executes immediately, and sets the result of runCommand() to be the onClick attribute value, nor can you do

execBtn.setAttribute("onclick", runCommand); 
like image 863
Aeon Avatar asked Sep 18 '08 19:09

Aeon


People also ask

Why is setAttribute not working?

The "setAttribute is not a function" error occurs for multiple reasons: calling the setAttribute() method on a value that is not a DOM element. placing the JS script tag above the code that declares the DOM elements. calling the setAttribute method on a jQuery object (should use attr() instead).

How do you use setAttribute?

setAttribute() Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() .

Is it bad to use onClick attribute?

It's a new paradigm called "Unobtrusive JavaScript". The current "web standard" says to separate functionality and presentation. It's not really a "bad practice", it's just that most new standards want you to use event listeners instead of in-lining JavaScript.

What is onClick property?

The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more. Using JavaScript, this event can be dynamically added to any element.


2 Answers

to make this work in both FF and IE you must write both ways:

     button_element.setAttribute('onclick','doSomething();'); // for FF     button_element.onclick = function() {doSomething();}; // for IE 

thanks to this post.

UPDATE: This is to demonstrate that sometimes it is necessary to use setAttribute! This method works if you need to take the original onclick attribute from the HTML and add it to the onclick event, so that it doesn't get overridden:

// get old onclick attribute var onclick = button_element.getAttribute("onclick");    // if onclick is not a function, it's not IE7, so use setAttribute if(typeof(onclick) != "function") {      button_element.setAttribute('onclick','doSomething();' + onclick); // for FF,IE8,Chrome  // if onclick is a function, use the IE7 method and call onclick() in the anonymous function } else {     button_element.onclick = function() {          doSomething();         onclick();     }; // for IE7 } 
like image 189
Shaike Katz Avatar answered Oct 14 '22 16:10

Shaike Katz


There is a LARGE collection of attributes you can't set in IE using .setAttribute() which includes every inline event handler.

See here for details:

http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html

like image 33
scunliffe Avatar answered Oct 14 '22 16:10

scunliffe