简体   繁体   中英

$.html is not having textarea value

When we are trying to get the html of div using the below code it is having all the data except the text which I am entering in the textarea or input field

var mywindow =  $(printDiv).html();

mywindow will have all the html except textarea text, radio button values or inputfield.

I have looked into this question but using clone is giving any text at all.

The thing to remember is that .html() does exactly what it says: it gives you the HTML currently in the DOM within that node. But the thing is, content entered by the user, and more generally the content of HTML input fields, is not a part of the HTML . Entering text into a textarea or checking a checkbox or radio button doesn't change the HTML one bit. It does update the internal memory representations of the individual DOM nodes, but this isn't represented in the HTML.

Whenever you want to get content from an input element on the page, you have to use .val() instead of .html() . The .val() function also does what it says: it gets you the value of the input field.

To get the text in a textarea you have to use .val() in JQuery. eg

var text = $('#textarea_id').val();
console.log(text);

The second line logs it to the console so you can check it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM