Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Fire two functions simultaneously

    How can you set an onChange() handler using the DOM to fire off two JS Functions simultaneously in IE? Can you?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Events are sequential, not simultaneous, in any application

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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
    }
    Last edited by brandonH; 11-02-2006 at 04:09 AM. Reason: typo's
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #4
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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:

    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;
    }

  • #6
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    -----> Bump. Anyone have a suggestion?

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    bump.

  • #8
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    ah, ok, so here's your problem.

    You're using the DOM 0 way of assigning event handlers
    Code:
    <input onclick="func()">
    and the DOM 1 way of assigning event handlers
    Code:
    document.form.inputfield.onclick = func;
    And what you need to do is use the DOM 2 method
    Code:
    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.
    Last edited by Beagle; 11-04-2006 at 04:37 PM. Reason: clarified difference between W3C and IE

  • #9
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    mybe u are looking for threads, i.e, multithreaed application. Can't tell if this can be done in java script. --> google for multithread

  • #10
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •