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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question calling a function?

    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

    Code:
    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

    Code:
    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.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    This
    Code:
    newSelectionDay.onchange =dayChange;
    or
    Code:
    newSelectionDay.onchange = function(){dayChange();}
    should work.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Yes but still no

    yes thank you, that did get rid of the error message but i still have a problem.

    Code:
    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

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Why don't you add a html attribute onchange="dayChange();" directly to the element you need?

    more clear:

    Code:
    newSelectionDay.setAttribute('onchange',"dayChange();");
    this don't work?

    best regards
    Last edited by oesxyl; 12-28-2007 at 01:55 AM.

  • #5
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    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.


  •  

    Posting Permissions

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