add event handler to element created using JS

Discussion in 'JavaScript' started by obiron, Feb 20, 2007.

  1. #1
    Having given up on trying to get IE6 to add innerHTML containing additional elements, I am now creating each element and adding it to the DOM manually.

    One of the elements needs to take an onkeyup event which then calls the JS and runs an AJAX call. When dynamically generating the original source HTML using PHP the HTML looks like this.

    <input type="text" name = "name_2" id = "name_2" size=20 onkeyup="fn_getCustomerInfo(2);"/>
    HTML:
    Where the 2 is passed through the ajax call to determine the target of the results and is therefore different on each line.

    I can set the type, name and id in JS using
    elementobj.name = 'name_' + nextcounter;
    elementobj.id = 'name_' + nextcounter
    elementobj.size='20';
    Code (markup):
    how do I set the onkeyup event handler?
     
    obiron, Feb 20, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Try:
    
    elementobj.onkeyup = function()
    {
        fn_getCustomerInfo(2);
    }
    
    Code (javascript):
     
    nico_swd, Feb 20, 2007 IP