I need select's option to be actually selected on document ready depending on an input's value.
Example:
<select id="mySelect">
<option value="1">myVal1</option>
<option value="2">myVal2</option>
</select>
<input type="text" id="myId" value="2">
For example, we've got a predefined value '2' of the input on page load. I need select-box to be auto-selected with it's option having the same value as input 'myId'. I mean 'mayVal1' must be selected.
When input has no value select must behave as default.
Thanks!
something like below should work///
in vanilla JS:
with jQuery:
Your mention of "on document ready" sort of suggests that you might be using jQuery, with that assumption made (albeit perhaps erroneously), I offer you this:
With a demo at JS Fiddle.
Edited in response to OP's question:
Right, given the disparity between your posted example and your question, I'm going to try to address both of the possibilities.
First, to select an
option
based on its text component (in your above code, the 'myVal1','myVal2' bits):JS Fiddle demo: note that the text component of the
option
element does not represent the value of theoption
element.Secondly, if you want to link the first part of the value entered into the
myId
input element to the value of theselect
/option
element(s):JS Fiddle demo.
These demos could work with
keyup()
, or equivalent actions/events, butsubmit()
seemed the better choice.