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
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    addEventListener problem

    Does anybody know why this wont work for me?

    I am trying to hide the second drop down if "4+" is chosen in the first drop down.

    I need to use addEventListener for this.

    Thanks for your help!

    <script language="JavaScript">

    function attachHandlers() {
    var ELMX = document.getElementById('Q5a_1');
    if (document.addEventListener){
    ELMX.addEventListener("change", close_other_box, false);
    } else {
    ELMX.attachEvent("onchange", close_other_box);
    }
    }

    attachHandlers();

    function close_other_box(){
    if(document.getElementById('Q5a_1').selectedIndex==5){
    document.getElementById('Q5b_1').disabled=true}
    else{
    document.getElementById('Q5b_1').disabled=false}}

    </script>




    <table><tr>
    <td><font face="verdana" size="-1" color="#990033">Home to work:</font>&nbsp;</td>
    <td align="center">&nbsp;&nbsp;<select tabindex="1" name="Q5a_1" id="Q5a_1">
    <option value="">Please select your answer</option>
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4+</option>
    </select>
    </td>
    <td align="center">&nbsp;&nbsp;<select tabindex="2" name="Q5b_1" id="Q5b_1">
    <option value="">Please select your answer</option>
    <option value="1">0-9</option>
    <option value="2">10-19</option>
    <option value="3">20-29</option>
    <option value="4">30-39</option>
    <option value="5">40-49</option>
    <option value="6">50-59</option>
    </select>
    </td>
    </tr>

    </table>

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    It's because your function is running even if your page haven't been fully loaded yet.

    This part:
    Code:
    attachHandlers();
    ...should be replaced with:
    Code:
    onload=attachHandlers;
    ...or if you want to use DOM 2.0 way...

    Then:
    Code:
    window.addEventListener?window.addEventListener('load',attachHandlers,false):
    window.attachEvent('onload',attachHandlers);
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help.

    I have got it to work in firefox, but it wont work in IE.

    I created a function so that I can use it for multiple drop-downs if need be.

    Do you have any idea why this wont work in IE ???

    Here is the new code:

    <script language="JavaScript">

    function AttachEvent(obj,evt,evtb,fnc,useCapture){
    if (obj.addEventListener){
    obj.addEventListener(evt,fnc,useCapture);
    return true;
    }else{
    obj.attachEvent(evtb,fnc);
    return true;
    }
    }


    window.onload=AttachEvent(window,'change', 'onchange', close_other_box, false);

    function close_other_box(){
    if(document.getElementById('Q5a_1').selectedIndex==5){
    document.getElementById('Q5b_1').disabled=true}
    else{
    document.getElementById('Q5b_1').disabled=false}}

    </script>

    <table><tr>
    <td><font face="verdana" size="-1" color="#990033">Home to work:</font>&nbsp;</td>
    <td align="center">&nbsp;&nbsp;<select tabindex="1" name="Q5a_1" id="Q5a_1">
    <option value="">Please select your answer</option>
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4+</option>
    </select>
    </td>
    <td align="center">&nbsp;&nbsp;<select tabindex="2" name="Q5b_1" id="Q5b_1">
    <option value="">Please select your answer</option>
    <option value="1">0-9</option>
    <option value="2">10-19</option>
    <option value="3">20-29</option>
    <option value="4">30-39</option>
    <option value="5">40-49</option>
    <option value="6">50-59</option>
    </select>
    </td>
    </tr>

    </table>

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    The obj that you would refer should be the ID of the select element, and not the window.

    ...with that said, change this part:
    Code:
    window.onload=AttachEvent(window,'change', 'onchange', close_other_box, false);

    ...to:
    Code:
    window.onload=function(){
    	AttachEvent(document.getElementById('Q5a_1'),'change', 'onchange', close_other_box, false);
    }
    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

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

    Thumbs up

    Brilliant! Thank you!


  •  

    Tags for this Thread

    Posting Permissions

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