Friday, May 24, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  28] [ 7]  / answers: 1 / hits: 17164  / 11 Years ago, fri, august 2, 2013, 12:00:00

I'm trying to change a value from a select tag using JavaScript. Let's say that I have this textbox, and if that textbox is null, no changes will be done and the value of the select tag options will be as is. But if that textbox is filled, then I have to assign a different value aside from the ones in the select tag options.



Here's what I'm trying to do:



HTML:



<input type=text id=txtTest />
<select name=rdoSelect id=rdoSelect>
<option value=option1>Option 1</option>
<option value=option2>Option 2</option>
</select>


JavaScript:



if (document.getElementById('txtTest').value===null)
{
document.getElementById('rdoSelect').value;
}
else
{
document.getElementById('rdoSelect').value = option 3;
}


I can't make it work. I've tried pointing it to an element/variable rather than to a value and it still doesn't work:



var test = document.getElementById('rdoSelect');
test.value = option 3;


I need help, please. Thanks!


More From » select

 Answers
29

Try using SelectIndex method. Please refer the below code.



I added OnChange event to input text to test this sample.



<html>
<head>
<script language=javascript>

function test()
{
if (document.getElementById('txtTest').value=='')
{
document.getElementById(rdoSelect).selectedIndex = 0;
}
else
{
document.getElementById(rdoSelect).selectedIndex = 1;
}
}

</script>
</head>
<body>
<input type=text id=txtTest onchange=test(); />
<select name=rdoSelect id=rdoSelect>
<option value=option1>Option 1</option>
<option value=option2>Option 2</option>
</select>
</body>
</html>

[#76571] Thursday, August 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brendan

Total Points: 426
Total Questions: 110
Total Answers: 94

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;