Add text box with div value

I am trying to add an input text box, and its value is a div value.

Here is what I came up with so far:

$(this).append('<input type="text" value=' $('#div1').val() '>'); 
+8
jquery append
source share
4 answers

Do not use HTML strings for everything!

 $(this).append( $('<input>', { type: 'text', val: $('#div1').text() }) ); 
+15
source share
 $(this).append('<input type="text" value='+ $('#div1').html()+ '>'); 
+2
source share
 $(this).append('<input type="text" value=' + $('#div1').val() + '>'); 

do not forget to contact +

It is also assumed that $ (this) is the actual object.

+1
source share

 <div id="parent-dv"> <lable for="posting">POST</lable> <textarea style="width:400px; height:auto;"></textarea> <input type="Submit" id="inputsubmit"> <button id="clear">Clear</button> </div> 

 $(document).ready(function(){ $('#inputsubmit').on('click',function(event) { var $inpute2xt = $("#parent-dv").find('textarea').val(); $('#parent-dv').append("<p></p>").addClass("newp").append($inpute2xt); } ); } ); 
 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <div id="parent-dv"> <lable for="posting">POST</lable> <textarea style="width:400px; height:auto;"></textarea> <input type="Submit" id="inputsubmit"> <button id="clear">Clear</button> <p></p> </div> </body> </html> 

0
source share

All Articles