0

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.

Community
  • 1
  • 1
antnewbee
  • 1,779
  • 4
  • 25
  • 38

2 Answers2

2

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.

Ken Bellows
  • 6,711
  • 13
  • 50
  • 78
  • Thanks, but we need to print the form before it is saved. What do you suggest. There are a lot of fields in form and we are using generic method to print the page. – antnewbee Apr 15 '16 at 11:55
  • If you need to print *only* the form (if you want the whole page, just use window.print()), here's a couple options: (1) Use CSS `@media(print)` and special classes to print only certain sections of the page at a time (see http://stackoverflow.com/a/2618980/470925). (2) Use jQuery pretty easily to grab the values of all input field types using something like `var values={}; $formElem.find('input,select,textarea').each(function(){ values[this.name] = $(this).val() })`. Then you can use `$formElem.html()`, send the HTML to an iframe, fill the values back in for each field, and print the iframe. – Ken Bellows Apr 15 '16 at 12:50
1

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

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

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

James McCormac
  • 1,635
  • 2
  • 12
  • 26