|
|
| I've noticed that IE won't assign events to dynamically created elements... i.e. element = document.createElement('input'); element.setAttribute('type','button'); element.setAttribute('value','Click Me!'); element.setAttribute('onclick','doSomething();');.... Firefox will do the onclick, but IE just sits there looking stupid... anyway to get it to work? |
| kadamat, I think it's just a matter of timing. Try setting the onclick property on the object instead of the attribute Maybe setting the attribute only has any effect if it's set when it's added to the doc in IE? |
| I tried both "element.onclick = 'doSomething();'" and assigning the element an id attribute and then doing "document.getElementById('elementsID').onclick = 'doSomething();"... IE was still no go, but Firefox didn't work anymore either |
| Hmm, are you sure? Try adding the element to the document before setting the event handler. Then use a function object rather than a string and use .onclick property |
| There are other ways of adding events And IE doesn't support the changing of some attributes for some elements I noticed that when i tried changing a password fiels to text and the other way around |
| Uncomment the line where setAttribute('onclick'... is used and try it in FF... it'll work, but any of the other ways won't work in FF or IE |
| Romans5n, have you tried button.onclick = function() { alert( ... And in any case, it's an input element outside of a form. Have you tried a button element instead? Or an input inside a form? |
| autos06,haha, no, and I just found a site that said to do just that ;)... turns out IE won't dynamic add events through setAttribute... instead you have to use the proprietary attachEvent() function... but then you're not cross-browser anymore... stupid IE ;) |
| I would also assume that Mozilla DOM-events style, addEventListener() works? (But only in browsers which support that) |
| Although I wonder if I can feature-sense the attachEvent? |
| Yes, I should imagine so Events aren't really that portable anyway, because the way that NS4 and MSIE do it is different, as is DOM Hence lots of old scripts assume that it's either NS4 or IE |