Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting innerHTML vs. setting value with Javascript

Tags:

javascript

dom

When you want to use Javascript to change HTML, how do you know when to use either of the following?

document.getElementById("randomNumber").value = number; document.getElementById("randomNumber").innerHTML = number; 
like image 700
node ninja Avatar asked Jan 11 '12 17:01

node ninja


People also ask

What is the difference between innerHTML and value?

value gives you the currently-set value of a form element (input, select, textarea), whereas . innerHTML builds an HTML string based on the DOM nodes the element contains.

Why you should not use innerHTML in JavaScript?

The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies.

What is the disadvantage of using innerHTML in JavaScript?

Disadvantages of innerHTMLIt is very slow because as inner HTML already parses the content even we have to parse the content again so that's why it takes time. When we have used the event handlers then the event handlers are not automatically attached to the new elements created by innerHTML.

How does innerHTML set value?

To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.


1 Answers

Setting the value is normally used for input/form elements. innerHTML is normally used for div, span, td and similar elements.

Here is a link showing the use of ID.value: http://www.javascript-coder.com/javascript-form/javascript-form-value.phtml

like image 50
Jay Tomten Avatar answered Oct 09 '22 04:10

Jay Tomten