I want to add a value to the input
field when it is append to DOM.
var strarray = [ "web developer", "web designer" ];
for (i = 0; i <= strarray.length-1; i++) {
j = [{ 'emp': strarray[i] }];
var a = j[0]['emp'];
console.log(a);
$("<input type='text' value=" + a + "/>")
.attr("id", "myfieldid"+i)
.attr("name", "myfieldid[]")
.appendTo(".mycal");
}
jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.
Answer: Use the jQuery val() Method You can simply use the jQuery val() method to set the value of an input text box.
append( function ) A function that returns an HTML string, DOM element(s), text node(s), or jQuery object to insert at the end of each element in the set of matched elements.
Add New HTML Contentappend() - Inserts content at the end of the selected elements. prepend() - Inserts content at the beginning of the selected elements. after() - Inserts content after the selected elements. before() - Inserts content before the selected elements.
There is a Input Element, the task is to set its value using JQuery. Here are a few examples discussed. To understand example first few methods to know. JQuery val() method: This method return/set the value attribute of selected elements. If we use this method to return value, it will return the value of the FIRST selected element.
The elements are created with text/HTML, jQuery, and JavaScript/DOM. Then we append the new elements to the text with the append () method (this would have worked for prepend () too) : var txt2 = $ ("<p></p>").text("Text."); // Create with jQuery txt3.innerHTML = "Text."; The jQuery after () method inserts content AFTER the selected HTML elements.
Firstly, add the jQuery, and Bootstrap CDN to the script or download them to your local machine. Create an input group and add an input field along with a button. i.e this button will be used to delete this input field. Create a button and on clicking this button we will dynamically add the input fields.
We will look at four jQuery methods that are used to add new content: append () - Inserts content at the end of the selected elements prepend () - Inserts content at the beginning of the selected elements after () - Inserts content after the selected elements
You can use
var input = $("<input/>", {
"type": 'text',
'value': a,
'id': "myfieldid" + i,
'name': "myfieldid[]"
});
$(".mycal").append(input);
Fiddle
You can create element using jQuery
var strarray = ["web developer", "web designer"];
for (i = 0; i <= strarray.length - 1; i++) {
j = [{
'emp': strarray[i]
}];
var a = j[0]['emp'];
console.log(a);
$("<input>", {
'type': 'text',
'value': a,
'id': "myfieldid" + i,
'name': "myfieldid[]"
}).appendTo(".mycal");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class=mycal></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With