I am trying to store a text value into an input tag from two select tags, where the value of second select tag depends on the first one. There's a javascript running for this , which looks something like this:
<script>
function byId(e) {return document.getElementById(e);}
function categoryComboChange()
{
var combo1 = byId('categoryCombo');
var combo2 = byId('subcategoryCombo');
alert(combo1.value);
emptyCombo(combo2);
switch(combo1.value)
{
case '-1': addOption(combo2, -1, '-select category first-');
break;
case '0': addOption(combo2,0,'Nokia');
addOption(combo2, 1, 'Samsung');
addOption(combo2, 2, 'Sony');
addOption(combo2, 3, 'Iphone 5');
addOption(combo2, 4, 'Lava');
addOption(combo2, 5, 'Micromax');
break;
case '1': addOption(combo2, 2, 'Edge Router');
addOption(combo2, 3, 'Subscriber Edge Router');
addOption(combo2, 4, 'Inter-provider Border Router');
addOption(combo2, 5, 'Core Router');
addOption(combo2, 6, 'Wired and Wireless Routers');
break;
case '2': addOption(combo2, 4, 'MTS');
addOption(combo2, 5, 'Docomo');
addOption(combo2, 5, 'Airtel');
addOption(combo2, 5, 'Photon');
addOption(combo2, 5, 'Idea');
addOption(combo2, 5, 'Vodafone');
break;
}
subcategoryComboChange();
}
function subcategoryComboChange()
{
var Category = document.getElementById('subcategoryCombo').value;
// Used desired display of following commands
document.getElementById('cvalue').value = Category;
}
function emptyCombo(e)
{
e.innerHTML = '';
}
function addOption(combo, val, txt)
{
var option = document.createElement('option');
option.value = val;
option.title = txt;
option.appendChild(document.createTextNode(txt));
combo.appendChild(option);
}
</script>
and my jsp page with required elements looks like this:
<div class="element">
<label for="category">Category </label>
<select id='categoryCombo' onchange='categoryComboChange();'>
<option value='-1' title='-select one-'>-select one-</option>
<option value='0' title='Mobile'>Mobile</option>
<option value='1' title='Nsw'>Routers</option>
<option value='2' title='Tas'>Datacard</option>
</select>
</div>
<div class="element">
<label for="category">Sub-Category </label>
<select id='subcategoryCombo' name="category" onchange='subcategoryComboChange();'>
<option value='-1' title='-select category first-'>-select category first-</option>
</select>
</div>
<div class="element">
<input id="cvalue" name="cval" class="text err" />
</div>
Currently, I am able to get the value in form of number like 0,1,2, depending on the choice, but I need to show text value in the input tag, like say for Mobile, it should show either nokia, samsung, etc. Can anyone suggest where I need to change my code in order to implement this task?
Try:
function subcategoryComboChange()
{
var Category = document.getElementById("subcategoryCombo");
var subCat = Category.options[Category.selectedIndex].text;
document.getElementById('cvalue').value = subCat;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.