10

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:

http://jsfiddle.net/4XR8M/

1
  • 1
    [] characters are not valid in html id property Commented Sep 6, 2013 at 10:18

7 Answers 7

26

You can do it as below.

$(this).prev('input').val("hello world");

Live Demo

Sign up to request clarification or add additional context in comments.

1 Comment

This is not really working anymore, try clicking on live demo, it just replace the input rather than adding value to the input
3

You can simply use

 $(this).prev().val("hello world");

JSFiddle Demo

2 Comments

This is not really working anymore, try clicking on live demo, it just replace the input rather than adding value to the input
@faize, Just checked fiddle. It's working as expected. Can you share more information?
3

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/

Comments

0
$.each(obj, function(index, value) {
    $('#looking_for_job_titles').tagsinput('add', value);
    console.log(value);
});

1 Comment

While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
0

You can simply use $(this).prev().val("hello world");

Comments

0

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#10").click(function(){
    $("input:text").val("10");
  });
  $("#20").click(function(){
    $("input:text").val("20");
  });
  $("#30").click(function(){
    $("input:text").val("30");
  });
  $("#40").click(function(){
    $("input:text").val("40");
  });
  $("#50").click(function(){
    $("input:text").val("50");
  });
  
});
</script>
</head>
<body>

<p>Name: <input type="text" name="user"></p>

<button id="10">Apply Discount 10%</button><br>
<button id="20">Apply Discount 20%</button><br>
<button id="30">Apply Discount 30%</button><br>
<button id="40">Apply Discount 40%</button><br>
<button id="50">Apply Discount 50%</button><br>

</body>
</html>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0
<input type="text" class="form-control" placeholder="Enter Amount" id="input">

<div>
 <span class="amt-block setText" data-amt="10">10</span>
 <span class="amt-block setText" data-amt="20">20</span>
 <span class="amt-block setText" data-amt="30">30</span>
 <span class="amt-block setText" data-amt="40">40</span>
 <span class="amt-block setText" data-amt="50">50</span>
</div>

<script> 
    $(".setText").click(function(event) { 
        amount = $(this).data('amt');
        // console.log(amount);
        $('#input').val(amount); 
    }); 
</script> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.