PDA

View Full Version : If function between combo box and text value



vvomble
12-05-2011, 05:45 PM
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


<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

jscheuer1
12-06-2011, 03:24 AM
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:


<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:


<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):


document.getElementById('text23').value += '\n' + selected;

Or for innerHTML:


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.