...

View Full Version : calling a function?



rempires
12-27-2007, 09:46 PM
Hi every, i have a javascript question regarding calling a function from a dynamically added html element.

i put the element into a variable and i need to add an onchange call to it

i know i can't get it to work in IE i haven't tested Firefox yet(my site keeps blocking me, stupid host... it thought i was flooding it because i accessed to so many times really fast.... so I'm running a IE specific proxy)

anyway the code looks something like


function addDDL()
{
var newSelectionDay = document.createElement('select');
newSelectionDay.setAttribute('id',DDLNum+"DDLDay");
newSelectionDay.setAttribute('name',DDLNum+"DDLDay");
newSelectionDay.onchange =dayChange();

//blah all we do ehre is add some options to that drop down lsit
NewDiv.appendChild(newSelectionDay);

}


that doesn't work though, the error is obviously this line



newSelectionDay.onchange =dayChange();


anyone know how i could add an event to a dynamically added element?

thanks,

john

p.s. dayChange is a function to, if you need that code i can post it, i just didn't think it was relevant since i think the error is in how i call it.

_Aerospace_Eng_
12-27-2007, 09:51 PM
This

newSelectionDay.onchange =dayChange;
or

newSelectionDay.onchange = function(){dayChange();}
should work.

rempires
12-28-2007, 01:35 AM
yes thank you, that did get rid of the error message but i still have a problem.


function dayChange()
{
alert("i work");
}

if i make that the function it never calls it. There are no errors generated i'm running firebug(Firefox add-on) to catch them. So apparently the onChange event is never called... can you make dynamically added elements have onChange events??

the code is the same as above except i made the change "_Aerospace_Eng_" gave me. i dropped the "()" if somone could please point me in the right direction here all i'm trying to do is add an onChange property to a drop down box that is dynamically added.

if you do need me to copy and paste all my code, or have any question, just ask. This is obviously my first time dealing with dynamically adding elements, cool but i seem to be having some issues.

thanks,

john

oesxyl
12-28-2007, 01:47 AM
Why don't you add a html attribute onchange="dayChange();" directly to the element you need?

more clear:



newSelectionDay.setAttribute('onchange',"dayChange();");


this don't work?

best regards

rempires
12-28-2007, 02:28 AM
ahh, because i never thought of that one... like i said new to this not thinking logically.

Thanks everyone for your help, that did work, i appreciate it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum