Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript innerHTML adding instead of replacing

quick question, i know we can change the content of a

<div id="whatEverId">hello one<div> by using:

document.getElementById("whatEverId").innerHTML="hello two"; 

now, is there a way I can ADD stuff to the div instead of replacing it??? so i can get

<div id="whatEverId">hello one hello two<div>

(using something similar of course)

like image 752
Gmo Avatar asked Jul 06 '11 09:07

Gmo


People also ask

Can you += innerHTML?

Appending to innerHTML is not supported: Usually, += is used for appending in JavaScript. But on appending to an Html tag using innerHTML, the whole tag is re-parsed.

Does innerHTML overwrite?

Yes, setting the . innerHTML properties overwrites any previous value for that property.

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.

How do you append innerHTML?

To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.


1 Answers

<div id="whatever">hello one</div> <script> document.getElementById("whatever").innerHTML += " hello two"; </script> 
like image 91
jcomeau_ictx Avatar answered Sep 20 '22 18:09

jcomeau_ictx