0

I have the following script that dynamically builds text fields with a value from other input element. The value is displayed on the screen but it doesn't get inserted into value="" as inspected in the dev tool. I need the value to be inserted as value="" so that the form data can be persisted.

$(document).on('click', 'button', function(event) {
  $("fieldset").append($('<input>').prop('type', 'text').val($('#entry').val()));
});

See my image

2
  • 2
    $('<input>').attr('value', $('#entry').val()) Commented Dec 24, 2014 at 9:44
  • 1
    Thank you. I forgot about attr. If you make this as an answer, I can settle this question. Commented Dec 24, 2014 at 9:52

2 Answers 2

3

You need to use .attr()

Use:

 $('<input>').attr('value', $('#entry').val())
Sign up to request clarification or add additional context in comments.

1 Comment

we can use $("input") also without < and > and input with $("input").val($('#entry').val()) also
2
$(document).on('click', 'button', function(event) {
    $("fieldset").append($("input[type = 'text']")).val($('#entry').val());
    });

Hope this helps you.

1 Comment

My original question uses val() but it doesn't create value attribute neither assign value into it.

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.