say I have a dropdown list like this:
<select id="MyDropDown"> <option value="0">Google</option> <option value="1">Bing</option> <option value="2">Yahoo</option> </select>
and I want to set the selected value based on the option text, not the value with javascript. How can I go about doing this? For example, with c# I can do something like the example below and the the option with "Google" would be selected.
ListItem mt = MyDropDown.Items.FindByText("Google"); if (mt != null) { mt.Selected = true; }
Thanks in advance for any help!
getElementById('stuff'). options[0]. innerHTML = "Water"; ... which get one select but on Id and not name rather than what was given: document. getElementsByName('stuff')[0].
You can bind a selected value from the drop-down list using the ngModel and edit it inside the text box or numeric input box. The selected item text and value can be got using the change event of the drop-down list through the args. itemData.
4.2) to set the 'selected' attribute of a select list based on its 'text' description rather than its 'value': $("#my-Select option[text=" + myText +"]"). attr("selected","selected") ; This code worked fine, until I noticed one select list on which it failed, depending on the text that was being matched.
var textToFind = 'Google'; var dd = document.getElementById('MyDropDown'); for (var i = 0; i < dd.options.length; i++) { if (dd.options[i].text === textToFind) { dd.selectedIndex = i; break; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With