Can anyone tell me what innerHTML is doing in javascript and give me example how I can use it?
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 .
Use innerHTML when you're setting text inside of an HTML tag like an anchor tag, paragraph tag, span, div, or textarea.
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.
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.
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With