I am using a dropdown to populate a textbox. But if preferred value is not present in dropdown then user directaly enter value in that textbox.
If user selects value from dropdown first and then he don't want that value and he types another text in that textbox then at this time the dropdown should be set to index 0 as user is typing another value.
I used textchanged event of textbox but it is not working. Can anyone tell me how to write javascript for this? Thanks in advance.
I am using a dropdown to populate a textbox. But if preferred value is not present in dropdown then user directaly enter value in that textbox.
If user selects value from dropdown first and then he don't want that value and he types another text in that textbox then at this time the dropdown should be set to index 0 as user is typing another value.
I used textchanged event of textbox but it is not working. Can anyone tell me how to write javascript for this? Thanks in advance.
Share Improve this question asked Dec 19, 2008 at 6:28 Devashri B.Devashri B. 2,7138 gold badges28 silver badges40 bronze badges 2- Setting the index to 0 would actually means selecting the 1st item right? – faulty Commented Dec 19, 2008 at 7:18
- yes. I have kept 1st item as " -Select-" – Devashri B. Commented Dec 19, 2008 at 9:26
1 Answer
Reset to default 10This should work for you:
function ResetDropDown(id) {
document.getElementById(id).selectedIndex = 0;
}
function ResetTextBox(id) {
document.getElementById(id).value = '';
}
<select id="MyDropDown" onchange="ResetTextBox('MyTextBox');">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input id="MyTextBox" type="text" onkeypress="ResetDropDown('MyDropDown');"/>