Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append input text field with value of a div

Tags:

jquery

append

I'm trying to append an input text field and its value to be the value of a div.

Here is what I came up so far:

$(this).append('<input type="text" value=' $('#div1').val() '>');
like image 983
jQuerybeast Avatar asked Jan 06 '12 20:01

jQuerybeast


People also ask

Can you put a div in a input?

You can't place any element inside an input. An input does not contain any HTML.

How do you add to the input field?

To append an element with a text message when the input field is changed, change() and appendTo() methods are used. The change() method is used to detect the change in the value of input fields.


2 Answers

Don't use HTML strings for everything!

$(this).append(
    $('<input>', {
        type: 'text',
        val: $('#div1').text()
    })
);
like image 128
Ry- Avatar answered Oct 28 '22 23:10

Ry-


$(this).append('<input type="text" value='+ $('#div1').html()+ '>');
like image 4
Royi Namir Avatar answered Oct 28 '22 23:10

Royi Namir