I want to add some value in an input field with jQuery. The problem is with the ID of the input field. I am using the id such as options[input2]
. In that case my code does not work. If I use ID like input2
, then it works fine. I need to use options[input2]
, how can I fix the code?
HTML:
<div>
<h3>Working</h3>
<input id="input1" type="text" value="" />
<input class="button" type="button" value="Add value" />
</div>
<div>
<h3>Not working</h3>
<input id="options[input2]" type="text" value="" />
<input class="button" type="button" value="Add value" />
</div>
jQuery:
$('.button').click(function(){
var fieldID = $(this).prev().attr("id");
$('#' + fieldID).val("hello world");
});
Demo:
This question is related to
javascript
jquery
html