-3

I am writing a web application where i have to send HTML from the DOM along with values of respective text areas and textboxes. Currently , I am using innerHTML for accomplishing this.

Lets suppose we have an html script

<div id="abcd">
   <form id="form1" >
      <input type="text" id="input1" />
   </form>
</div>

Now, on the html page in browser, I fill the text box with some value (say - "12345"). And, I try to capture complete html using

var x = $("#abcd").html();  // or 
var x = document.getElementById("abcd").innerHTML ;

All I get in x is

<div id="abcd">
   <form id="form1" >
      <input type="text" id="input1" />
   </form>
</div>

Although I wanted something like -

<div id="abcd">
   <form id="form1" >
      <input type="text" id="input1" value="abcd" />
   </form>
</div>
porges
  • 30,133
  • 4
  • 83
  • 114
Atul
  • 874
  • 1
  • 7
  • 17

1 Answers1

0

You are using wrong selector to get the area of text input. Try to use "#input1". Like this to get your text area value

var x = $("#input1").val();

This will capture the text entered by user. Then use variable x to create compleete HTML

var y = '<div id="abcd"><form id="form1" ><input type="text" id="input1" value=' + x + "/>   </form></div>"

Here is fiddle http://jsfiddle.net/qMcJ9/ PS. I do not know what event you are using so I connected it t .keyup event, You can change it any way you want

Maxim Ershov
  • 1,284
  • 1
  • 11
  • 15
  • I have around 20 input fields, so doing this manually is cumbersome. I mean we can do that, but I was hoping if there is already built functions to do so. – Atul Jul 11 '14 at 06:33
  • Then just save your values into an array (id -> value) and then assign them back to each input field. Check this answer http://stackoverflow.com/questions/169506/obtain-form-input-fields-using-jquery – Maxim Ershov Jul 11 '14 at 14:01