Adding value to input field with jQuery
You can simply use
$(this).prev().val("hello world");
JSFiddle Demo
You have to escape [
and ]
.
Try this:
$('.button').click(function(){
var fieldID = $(this).prev().attr("id");
fieldID = fieldID.replace(/([\[\]]+)/g, "\\$1");
$('#' + fieldID).val("hello world");
});
Demo: http://jsfiddle.net/7RJtf/1/
You can do it as below.
$(this).prev('input').val("hello world");
Live Demo