Results 1 to 2 of 2

Thread: If function between combo box and text value

  1. #1
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If function between combo box and text value

    i'm desperately seeking some javascript advice.

    I'm currently using wysiwyg webbuilder 8.

    I have one of my combo boxes linking to another but i want the first box to affect text on the page the text id is text23 and here is the coding i am using at the moment

    Code:
    <script type="text/javascript">
    function fillSecondCombo()
    {
       var combo1 = document.getElementById('Combobox3');
       var combo2 = document.getElementById('Combobox4');
       var selected = combo1.options[combo1.options.selectedIndex].value;
       
       if (selected == 1)
       {
          combo2.options.length = 1;
          combo2.options[0] = new Option("999 Million Forza 3 Credits", "1");
    
       }
       else
       if (selected == 2)
       {
         combo2.options.length = 5;
         combo2.options[0] = new Option("999 Million Forza 4 Credits", "1");
         combo2.options[1] = new Option("750 Million Forza 4 Credits", "2");
         combo2.options[2] = new Option("500 Million Forza 4 Credits", "3");
         combo2.options[3] = new Option("400 Million Forza 4 Credits", "4");
         combo2.options[4] = new Option("300 Million Forza 4 Credits", "5");
         combo2.options[5] = new Option("200 Million Forza 4 Credits", "6");
         combo2.options[6] = new Option("100 Million Forza 4 Credits", "7");
         combo2.options[7] = new Option("75 Million Forza 4 Credits", "8");
    }
       else
       if (selected == 3)
       {
          combo2.options.length = 4;
          combo2.options[0] = new Option("2k Gamerscore Increase", "7");
          combo2.options[1] = new Option("5k Gamerscore Increase", "8");
          combo2.options[2] = new Option("10k Gamerscore Increase", "9");
          combo2.options[3] = new Option("25k Gamerscore Increase", "10");
    }
       else
       if (selected == 4)
       {
          combo2.options.length = 4;
          combo2.options[0] = new Option("1 Purchased", "7");
          combo2.options[1] = new Option("2 Purchased", "8");
          combo2.options[2] = new Option("3 Purchased", "9");
          combo2.options[3] = new Option("4 Purchased", "10");
       }
       else
       if (selected == 5)
       {
          combo2.options.length = 4;
          combo2.options[0] = new Option("1 Purchased", "7");
          combo2.options[1] = new Option("2 Purchased", "8");
          combo2.options[2] = new Option("3 Purchased", "9");
          combo2.options[3] = new Option("4 Purchased", "10");
       } 
       else
       if (selected == 6)
       {
          combo2.options.length = 4;
          combo2.options[0] = new Option("1 Purchased", "7");
          combo2.options[1] = new Option("2 Purchased", "8");
          combo2.options[2] = new Option("3 Purchased", "9");
          combo2.options[3] = new Option("4 Purchased", "10");
       }
      else
       {
          combo2.options.length = 0;
       }
    }
    </script>
    this is the only thing i need to complete the site i am working on

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If text23 is a textarea or text input, you want it's value property. You haven't said what you want from combo1 to appear in text23, but let's say it's its value, then:

    Code:
    <script type="text/javascript">
    function fillSecondCombo()
    {
       var combo1 = document.getElementById('Combobox3');
       var combo2 = document.getElementById('Combobox4');
       var selected = combo1.options[combo1.options.selectedIndex].value;
       document.getElementById('text23').value = selected;
       
       if (selected == 1)
       {
          combo2.options.length = 1;
          combo2.options[0] = new Option("999 M . . .
    If text23 is a span, div, p, etc., then you want it's innerHTML property:

    Code:
    <script type="text/javascript">
    function fillSecondCombo()
    {
       var combo1 = document.getElementById('Combobox3');
       var combo2 = document.getElementById('Combobox4');
       var selected = combo1.options[combo1.options.selectedIndex].value;
       document.getElementById('text23').innerHTML = selected;
       
       if (selected == 1)
       {
          combo2.options.length = 1;
          combo2.options[0] = new Option("999 M . . .
    See the red = signs in the above code blocks? If instead of replacing the value or text, you want to add to it, use += instead. You could even add a line break. Like for value (only works for textarea, a text input shows no line breaks):

    Code:
       document.getElementById('text23').value += '\n' + selected;
    Or for innerHTML:

    Code:
       document.getElementById('text23').innerHTML += '<br>' + selected;
    If you want more help:

    Please post a link to a page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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