4

I would like to put variable FLC in inside $("#FNC").html(''); but don't know how to, any tips?

DEMO:

var FLV = $("#random-input-box").val();
$("#FNC").html('<input id="random-input-box" style="width: 99px;" value="FLC" disabled="disabled" />');
1
  • there are many problems with your short code here: 1. when you are getting the val() of #random-input-box - I assume that tag with that ID is already created, at the same time you are trying to create another tag with the same ID (when you do html()). 2. your input tag has no type attribute. 3. the value that you are trying to use for the input has FLC, while the variable was called FLV. 4. papaiatis has show the correct way to add the variable's value with value="'+FLV+'" Commented Mar 11, 2012 at 15:14

2 Answers 2

3

Did you mean putting the value of FLV into the value attribute of the input field?

var FLV = $("#random-input-box").val();
$("#FNC").html('<input id="random-input-box" style="width: 99px;" value="'+FLV+'" disabled="disabled" />');
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I'm new to jquery and I couldn't find this simple answer on forums or stackoverflow at all :S
Actually it doesn't work, I tested in all ways possible. On execution <div id="FNC"></div> stays empty, instead of having input box inside :S
0

String concatenation is dangerous without input validation.

Just use .val() to set the input's value, and ideally use jQuery's constructor rather than .html() to create it:

var FLV = $("#random-input-box").val();
$('#FNC').empty();

$('<input id="random-input-box" style="width: 99px;" disabled="disabled" />')
    .val(FLV).appendTo('#FNC');

2 Comments

I have tryed your code, firebug doesn't show changed in code, it show <div id="FNC"></div> as empty no input field inside.
yeah, I copied one of the errors from your code. It should work now.

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.