What is the difference between document.getElementById ("test"). Value and document.getElementById ("test"). InnerHTML

document.getElementById("test").value document.getElementById("test").innerHTML 

Does the first mean the address, and the second mean the value stored at the address? Also, where can I find documentation on the value property?

+6
source share
5 answers

.value gives the current value of the form element ( input , select , textarea ), while .innerHTML builds an HTML string based on the DOM nodes contained in this element.

For a simple example, go to the JS Fiddle demo and enter a new value in input , and then go from input.

The test uses the following JavaScript:

 document.getElementById('input').onchange = function(){ alert('innerHTML: ' + document.getElementById('input').innerHTML + '; whereas value: ' + document.getElementById('input').value); }; 

(The above text has been updated, after a comment left by me , in the comments below.)

+10
source

some HTML elements have an attribute "value" , for example <input/> , some others do not have it.

if you want to change them, you can use the DOM attribute (used with Javascript ) innerHTML (if any). this attribute represents the content of an element, so it can be used for elements that accept the nest of another element, such as <div/> ,

+3
source

Many elements in HTML may have an identifier, so the definition of value will change for everyone.

value will be essentially what this element understands as value. For example, <input type=text> will provide you with text inside.

innerHTML will be what is inside the HTML code. For example, a <TR> has a TD child, plus everything that is there.

value and innerHTML can (usually) be written to as well as read.

+3
source

This is because some tags work based on their attributes, while others work on text between opening and closing tags.

.value retrieves any value for the value attribute of the tag. .innerHTML extracts everything between the open and close shortcut.

For example, if the HTML tag was <input type="text" value="Enter name here" id="user_name" />
and you used javascript
var name = document.getElementById('user_name').value
declares the name variable and sets it to "Enter a name here" (provided that the user has not changed it). On the other hand, if you have HTML, for example, <div id="abc">blah blah</div>
then you would use var text = document.getElementById('abc')
and that would set the text variable to blah blah.

+1
source
 document.getElementByid('test').value 

used to set the value in the text box. how

 <input type="text" id="test" name="test"> 

Now it puts the value in this feild text.

For now, document.getElementByid('test').innerHTML used to set the value in the given scope. how

 <div id="test"> </div> 

Now type the value in the div area.

0
source

Source: https://habr.com/ru/post/907179/


All Articles