Die 20
Die 20

Reputation: 261

How to insert a value in to the value property of a newly added input field?

I want to take the value of a jquery variable and add it to the value property of an input field. I thought you can use jquery variables with HTML by doing "+VARIABLE+"...but its not working. The variable name just appears and not the value.

Here is the jQuery :

var content_unique_id = $(this).closest('.content_unique_id').val();
    $('<input type="hidden" name="content_unique_id[]" value="+content_unique_id+" />').insertAfter('.content_container:last');

Upvotes: 0

Views: 103

Answers (3)

Blender
Blender

Reputation: 298246

Here's another way of doing it:

$('<input>', {
    type: 'hidden',
    name: 'content_unique_id[]',
    value: content_unique_id
}).insertAfter('.content_container:last');

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191749

You need to break out of the string so the value is interpolated:

$('<input type="hidden" name="content_unique_id[]" value="'
    + content_unique_id + '" />')

Alternatively you could use $("<input...>").val(content_unique_id)

Upvotes: 2

Memolition
Memolition

Reputation: 494

value="' + content_unique_id + '" add single quotes before and after + plus signs

Upvotes: 2

Related Questions