0

I have input box #bar inside of div #foo. The value of input box #bar is "bar"

<div id="foo">
    <input id="bar" type="text" name="bar" size="5" value="bar" style="width:200px"/>
</div>

I want to use jQuery to change the value of #bar from "bar" to "foobar".

I saw this question on stackoverflow: Insert javascript (jquery) variable in html input field (textbox)

and tried the below jQuery:

$(document).ready(function() {
    var fooBar = "foobar";
    $('#bar').val(data[0].fooBar);
});

I must be missing something. I tried searching google, but as the post above mentions it was not very helpful. here is a link to the fiddle.

2
  • Can you tell us why did you think data[0].fooBar should work Commented Mar 15, 2014 at 5:36
  • I am new to javascript. Did try to google, did search stackoverflow. Other stackoverflow post had data[0] in solution. Now I see that he must have been using an array somewhere. Thank you for your help Commented Mar 15, 2014 at 5:42

1 Answer 1

1

There is no variable called data, you have a variable called fooBar just use it

$(document).ready(function() {
    var fooBar = "foobar";
    $('#bar').val(fooBar);
});

Demo: Fiddle

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

1 Comment

This did work. The other poster must have been using the rest of the code for something else. Thank you!

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.