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 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Help adding id's to my function

    Hi,

    I have a piece of code that works fine on a single drop down box.

    However I need to use it within a page that has mulitple drop down boxes each with their own ID.

    So how do I add the ID after 'selectBox'.

    Code:
    <script type="text/javascript">
    
    function changeFunc() {
    
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    
    if (selectedValue == 2)
      {
    alert(selectedValue);
      }
    }
    
    </script>
    I have used the id's before like below but don't understand how to implement them in the code above.

    Code:
    document.getElementById("hideabr_"+id).style.display="inline";
    Thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,494 Times in 4,458 Posts
    Where, pray tell, are you getting selectBox from in the first place?

    Generally, for form elements, you should NOT use their ID. You should use their name.

    So it depends on whether you really want to use the ID or whether, better, you use the name.

    You also don't say where the numeric part of the ID/name from. I have chosen to pass it into the function as the argument to the function, which.

    I am assuming you have either <select id="selectBox73"> or <select name="selectBox28"> (or whatever number you are using).

    ID:
    Code:
    function changeFunc( which ) { 
        var sel = document.getElementById("selectBox" + which);
        var selectedValue = sel.value;
        if (selectedValue == 2) { alert(2); }
    }
    Name:
    Code:
    function changeFunc( whiich ) { 
        var frm = document.getElementById("idOfYourForm");
        var sel = frm["selectBox" + which];
        var selectedValue = sel.value;
        if (selectedValue == 2) { alert(2); }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks I got it working!

    The ID was coming from a PHP loop:

    PHP Code:
    <select name="byon" class="hidespan" onchange="byonFunc('<?php echo $id;?>');"  id="dotxtdate_<?php echo $id;?>">
    <option value="by">By:</option>
      <option value="on">On:</option>
    <option value="2">None</option>
    </select>
    Working code:
    Code:
    function byonFunc(id) {
    
    var sel = document.getElementById("dotxtdate_"+id); 
    var selectedValue = sel.value; 
    if (selectedValue == 2) { alert(2); }
    
    }
    Thanks for your help

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks I got it working!

    The ID was coming from a PHP loop:

    PHP Code:
    <select name="byon" class="hidespan" onchange="byonFunc('<?php echo $id;?>');"  id="dotxtdate_<?php echo $id;?>">
    <option value="0">0</option>
      <option value="1">1</option>
    <option value="2">2</option>
    </select>
    Working code:
    Code:
    function byonFunc(id) {
    
    var sel = document.getElementById("dotxtdate_"+id); 
    var selectedValue = sel.value; 
    if (selectedValue == 2) { alert(2); }
    
    }
    Thanks for your help


  •  

    Posting Permissions

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