jquery add value to dynamically created input text field

半腔热情 提交于 2019-12-04 11:58:15

Try:

$('body').find('#priority').attr('value', tplData);

EDIT : It should be work, however you can do this too:

$('body').find('name=["priority"]').val(tplData);
$('body').find('#priority').val(tplData);

EDIT: See this on jsfiddle

First thing is,is your textbox generated before you assign the value to it.?? You can check if any error is there in console tab.Sometimes what happen if any error is occurred before your code it will not fire.So right click on page->Inspect Element->Console.If any errors then solve it and retry.

you can try like $('#priority').val(tplData)

If you have multiple controls then u have to use class

$(".priority").prop("value",tplData);

check if any errors in console.

So, after further banging of my head against the proverbial code wall, I was able to engineer a solution that ended up working superbly!

The dynamically created textbox is now accepting assignment of a value to it from my jquery code. The magic was in rethinking exactly how the element must be seen by jquery in terms of visibility to the code.

First thing to account for is that the container DIV is static and always present in the document. This means that anything created in that DIV will strictly be client-side always. Locking into a client-side frame of mind then led me to thinking that jQuery needs to be made aware of the newly created elements. This was achieved by the following code;

var inputElm = '';
inputElm += '<input type="' + data.type + '" name="' + data.name + '" id="' + data.name + '" class="' + data.name + ' input-group vert-spacer-bottom" placeholder="' + data.placeholder + '" />';

$('.field-container').after().html(inputElm);

Now, when I execute the code...it works beautifully and sets the value of the input textbox to the desired value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!