简体   繁体   中英

InnerHTML issue

How do I get the values of textboxe's along with innerHTML ?

For example, if I have

<html>
   <head>
   </head>
        <body>
            <div id="getvalue">
          <p><input type="text" name="username" id="username"></p>
     </div>

       </body>
</html>

I need the innerHTML of the particlular id="getvalue" along with the textbox value. I got the result if i use

var gotvalue=document.getElementBYId('getvalue').innerHTML;

I just got the result

"<p><input type="text" name="username" id="username"></p>"

Also, I need the textbox value along with the innerHTML . How could i do this?

Thanks in advance.

You want the .value property of the <input> element, like this:

var gotvalue = document.getElementById('getvalue').innerHTML;
var inpitvalue = document.getElementById('username').value;

As i can only imagine one halfway good reason for what you need the innerHTML(put it somewhere else into the document for copying), I would suggest to use the DOM-method to get the copy: cloneNode(true) and also use a DOM-Method(which one depends on where to put) to inject it into the DOM.
cloneNode() will preserve the attributes too, so the value will also be copied.
If you really need the string, forget this ^^

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