Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what innerHTML is doing in javascript?

Can anyone tell me what innerHTML is doing in javascript and give me example how I can use it?

like image 733
Kareem Nour Emam Avatar asked Feb 02 '11 19:02

Kareem Nour Emam


People also ask

Should you 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. You can read the MDN documentation on innerHTML .

Where can I use innerHTML?

Use innerHTML when you're setting text inside of an HTML tag like an anchor tag, paragraph tag, span, div, or textarea.

What is the use of inner HTML property give example?

The innerHTML property can be used to write the dynamic html on the html document. It is used mostly in the web pages to generate the dynamic html such as registration form, comment form, links etc.

What is inner text in JavaScript?

The innerText property of JavaScript is used to get or set the text inside an HTML element and its descendants. The innerText has a similar working phenomenon to innerHTML. Both properties manipulate the content of an HTML element but with different aspects.


2 Answers

The innerHTML property is used to get or set the HTML content of an element node.

Example: http://jsfiddle.net/mQMVc/

     // get the element with the "someElement" id, and give it new content document.getElementById('someElement').innerHTML = "<p>new content</p>";       // retrieve the content from an element var content = document.getElementById('someElement').innerHTML;  alert( content ); 
like image 190
user113716 Avatar answered Sep 28 '22 01:09

user113716


The innerHTML property is part of the Document Object Model (DOM) that allows Javascript code to manipulate a website being displayed. Specifically, it allows reading and replacing everything within a given DOM element (HTML tag).

However, DOM manipulations using innerHTML are slower and more failure-prone than manipulations based on individual DOM objects.

like image 30
Michael Borgwardt Avatar answered Sep 28 '22 01:09

Michael Borgwardt