F. Flores F. Flores - 5 months ago 29
Javascript Question

How to assign a value created dynamicly

I am trying to assing a value from a input that is created dynamicly from ajax also the input recieves the id value with a variable here is my input:

html += '<input type="text" class="form-control hidden-print" id="' + picFile.name + '"/>';


And this is what I want to do:

var value = $('#' + picFile.name);


Then I show this

html+= '<label>' + value + '</label>';


But Im only get this: [Object][Object]

Example

Answer

More than likely you are not "creating" your string into real elements and therefor you can't access it's value. If that is the case, you could try creating the html string into an element first like this:

Assuming html in your ajax variable: // I don't know where you are getting picFile.name so make sure that object exists var val = $(html).find('#' + picFile.name).val();

Also, are you trying to append your label to the same html string and create both an input and a label element?

if so then: html+= '<label>' + val + '</label>';

If not: html = '<label>' + val + '</label>';