Create <select> tag with an onchange property

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akoymakoy
    New Member
    • Oct 2006
    • 42

    Create <select> tag with an onchange property

    Good Day

    I have these line of codes in my javascript
    ......
    Code:
    if (selObj.type == 'text' ) {
            parentObj = document.getElementById('destinationCity').parentNode;
            parentObj.removeChild(selObj);
            var inputSel = document.createElement("SELECT");
            inputSel.setAttribute("name","destinationCity");
            inputSel.setAttribute("id","destinationCity");        
            parentObj.appendChild(inputSel) ;
            selObj = document.getElementById('destinationCity');
            selObj.options[optionCounter++] = new Option('Select State','');
           
            selObj.selectedIndex = 0;
          }
    ........

    this will create a selectBox element named destinationCity

    the scenario is this...

    when i select a country with an appropriate list of cities found.. a selectbox will be created. when there is no cities listed for that country an inputbox will be created.

    i am able to change back and forth with this but i could not add an attribute onChange() to the created selectBox..

    is it possible for me to create an onChange() attribute to the selectbox wherein the result will be something like:
    <select name="city" onchange="javas cript:myFunctio n">

    i tried this but it wont work:

    inputSel.setAtt ribute("onChang e","myFunction( )");

    Thanks in advance
    Last edited by acoder; Nov 14 '08, 09:04 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    onChange should be 'onchange', it needs to be set to the function reference (not the result of executing the function, i.e. no brackets) and events don't get set in IE with setAttribute. Either set directly:
    Code:
    inputSel.onchange = myFunction;
    or use addEventListene r/attachEvent.

    Comment

    Working...