redho home | products | services
Web Design Forums

Web Design Forums  


Web Hosting, Web Design, Software and Web Development Forums  
 FAQFAQ   MemberlistArchive  Log inLog in   RegisterRegister 
         

IE wont assign events to dynamically created elements


Goto page 1, 2  Next
 
Post new topic   Reply to topic    Web Design Forums -> JavaScript forum
View previous topic :: View next topic  
Author Message
kadamat



Joined: 01 Jun 2006
Posts: 88
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?
  Reply with quote


autos06



Joined: 13 May 2006
Posts: 123
Location: New York
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?
  Reply with quote
kadamat



Joined: 01 Jun 2006
Posts: 88
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
  Reply with quote
autos06



Joined: 13 May 2006
Posts: 123
Location: New York
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
  Reply with quote
Koplaf



Joined: 28 May 2006
Posts: 130
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
  Reply with quote
kadamat



Joined: 01 Jun 2006
Posts: 88
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
  Reply with quote
autos06



Joined: 13 May 2006
Posts: 123
Location: New York
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?
  Reply with quote
kadamat



Joined: 01 Jun 2006
Posts: 88
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 ;)
  Reply with quote
autos06



Joined: 13 May 2006
Posts: 123
Location: New York
I would also assume that Mozilla DOM-events style, addEventListener() works?
(But only in browsers which support that)
  Reply with quote
kadamat



Joined: 01 Jun 2006
Posts: 88
Although I wonder if I can feature-sense the attachEvent?
  Reply with quote
Page 1 of 2 Goto page 1, 2  Next
Post new topic   Reply to topic    Web Design Forums -> JavaScript forum


Dubai Forums - Expat Help | Vegan Chat | Java Programming | Free 3D tutorials and 3d textures | Paris Forum | EU Forum
Free Dubai Classifieds | Free London Classifieds | Jobs in London

High Quality, Custom 3d animation and Web Design solutions Royal Quality Web Hosting Services Vegetarian and Animal Rights news

Powered by phpBB © 2001, 2005 phpBB Group