Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a value on input field when appending it to DOM using jquery

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");
}
like image 931
sourabh Avatar asked Sep 18 '15 09:09

sourabh


People also ask

How do you append something in jQuery?

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.

How can add value in textbox using jQuery?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to set the value of an input text box.

What jQuery function is used to insert markup into the DOM?

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.

How append HTML data to div in jQuery?

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.

How to set the value of input element in jQuery?

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.

How do I append elements to text using jQuery?

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.

How to add input field dynamically using jQuery and bootstrap?

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.

How to add new content using jQuery?

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


2 Answers

You can use

var input = $("<input/>", {
    "type": 'text',
    'value': a,
    'id': "myfieldid" + i,
    'name': "myfieldid[]"
});

$(".mycal").append(input);

Fiddle

like image 196
Anoop Joshi P Avatar answered Oct 17 '22 13:10

Anoop Joshi P


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>
like image 2
Pranav C Balan Avatar answered Oct 17 '22 12:10

Pranav C Balan