Reputation: 2228
I am trying to store a text value in an input element. The element is accessed by a jquery selector, where the variable selectedindex contains an integer from a dropdown list (it will contain either 0,1,2). Here is the code including the html and javascript
<li id="ID1">
<p><label>Parts:</label>
<input type="text" name="0" class="lab" />
<input type="text" name="1" class="lab" />
<input type="text" name="2" class="lab" />
</p>
</li>
var index = b.selectedIndex;
var usr_input = $("#spinner").spinner("value");
$("#ID1 > p > input[name$=index]").text(usr_input);
The problem is the selector doesn't find the specific input element using the variable 'index'. I hardcoded 0 into the selector for testing purposes $("#ID1 > p > input[name$=0]").text(usr_input);
and that worked as it selected the input with name 0. How can I implement this using a variable.
Upvotes: 1
Views: 674
Reputation: 104775
String concatenation, also use .val()
for text inputs:
("#ID1 > p > input[name$=" +index + "]").val(usr_input);
Upvotes: 1
Reputation: 28837
Try this:
$("#ID1 > p > input[name$="+index+"]").val(usr_input);
What you need is to concatenate that variable into your selector and you can do that by ending/starting the string with "
and using +
which concatenates it with the variable.
If you are targeting a input you should use .val()
Upvotes: 1