...

View Full Version : Fire two functions simultaneously



ruggeddesign
11-02-2006, 02:54 AM
How can you set an onChange() handler using the DOM to fire off two JS Functions simultaneously in IE? Can you?

mrhoo
11-02-2006, 02:56 AM
Events are sequential, not simultaneous, in any application

brandonH
11-02-2006, 03:07 AM
yes, technically every script/function is run one thing at a time, as mrhoo said "sequentially". but i think what you are asking is if you can run two functions from the same event, if so then yes you can.

onchange="firstfunction();secondfunction();"

or you can call the second function from within the first function.

function firstone(){
//place call to second function either at the top of the first function
//or at the very bottom, depending on when you want it to be triggered
secondone();

//do what you wnat for first function here....

}

unction secondone(){
//do what you wnat for second function here
}

ruggeddesign
11-02-2006, 08:21 PM
Thank you, that is what I was looking for. I am just looking to have two functions execute on an onchange. I knew that you could do it with the addEventListener() but, I hear that that does not work in IE.

I'll get on that then.

ruggeddesign
11-02-2006, 08:40 PM
Actually, I'm still not sure what to do. I've been having this problem in the background of the rest of my work for a week or so now, I was wondering if somebody could help. I basically have this form validation function that handles any errors my validation routine spits out, called writeERROR(). It takes the form element and changes it's onChange handler to removeERROR() so that when the users fix their mistakes the validation errors are removed. The problem is that I already have onChange functions hard-coded into some elements of the form. I need both functions to execute. I tried concatanating the removeERROR function to the .onChange property of the form element with a += operation but it did not work. Any suggestions would be appreciated. Here is the code:



function writeERROR(obj,message)
{
validForm = false;
if (obj.hasError) return;
if (W3CDOM)
{
obj.className += ' error';
// Store the old onChange handler for later re-insertion (temporary solution)
var oldOnChange = obj.getAttribute('onchange');
obj.oldOnChange = oldOnChange;

obj.onchange = removeERROR();
var sp = document.createElement('span');
sp.className = 'error';
sp.appendChild(document.createTextNode(message));
obj.parentNode.appendChild(sp);
obj.hasError = sp;
}
else
{
errorstring += obj.name + ': ' + message + '\n';
obj.hasError = true;
}
if (!firstError)
{
firstError = obj;
}
}

function removeERROR()
{
this.className = this.className.substring(0,this.className.lastIndexOf(' '));
this.parentNode.removeChild(this.hasError);
this.hasError = null;
var oldOnChange = this.oldOnChange;
this.onchange = oldOnChange;
this.click;
}

ruggeddesign
11-03-2006, 04:15 PM
-----> Bump. Anyone have a suggestion?

ruggeddesign
11-03-2006, 09:50 PM
bump.

Beagle
11-04-2006, 03:36 PM
ah, ok, so here's your problem.

You're using the DOM 0 way of assigning event handlers

<input onclick="func()">

and the DOM 1 way of assigning event handlers

document.form.inputfield.onclick = func;

And what you need to do is use the DOM 2 method


if (document.form.inputField.addEventListener)
{ // W3C standard
document.form.inputField.addEventListener('click',func1,false);
document.form.inputField.addEventListener('click',func2,false);
}
else if (document.form.inputField.attachEvent)
{ //IE
document.form.inputField.attachEvent('onclick',func1);
document.form.inputField.attachEvent(onclick',func2);
}


You should abstract away this conditional into your own function, as most programmers do for this specific purpose. It's no fun putting this conditional all over the place. Have a look at:

http://www.quirksmode.org/js/events_advanced.html

for more information.

BubikolRamios
11-05-2006, 04:52 PM
mybe u are looking for threads, i.e, multithreaed application. Can't tell if this can be done in java script. --> google for multithread

Beagle
11-06-2006, 03:01 AM
No, javascript has no explicit multithreading capabilities, only implicit ones, which do not make for easy using. Besides, multithreading is not necessary to solve the OP's problem.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum