Javascript Course


Get and Replace HTML content with innerHTML

innerHTML is a JavaScript property that can be used to get the HTML content added into a HTML element in web page, and also it can be used to replace the content into a HTML element with other content.


1. Syntax, get content with innerHTML:
var content = element.innerHTML;
- Example, adds the content of a Div in a textarea:
<div id='id1'>JavaScript
 <p>Example innerHTML - https://coursesweb.net/javascript/</p>
</div>
<p>If you click on the following button, it adds in textarea the content of the HTML element with id 'id1'.</p>
<button id='btn1'>Try it</button><br>
<textarea id='txta' cols='40' rows=4'></textarea>

<script>
var txta = document.getElementById('txta');

document.getElementById('btn1').addEventListener('click', (ev)=>{
 var cnt = document.getElementById('id1').innerHTML;
 txta.value = cnt;
});
</script>
2. Syntax, add /replace content with innerHTML:
element.innerHTML = 'HTML content';
- Example, replaces the content of a Div:
<div id='id1'>Div - JavaScript</div>
<p>If you click on the following button, it replaces the content of the Div above.</p>
<button id='btn1'>Try it</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 document.getElementById('id1').innerHTML ='<p>Example innerHTML - https://coursesweb.net/javascript</p>';
});
</script>

Get and Replace HTML content with outerHTML

outerHTML is a JavaScript property that can be used to get the string with a HTML element, including its tag, attributes and child elements, also it can replace a HTML tag (including its content) with other HTML content.


1. Syntax, get HTML content with outerHTML:
var content = element.outerHTML;

The difference between innerHTML and outerHTML is that the innerHTML gets the content added into a HTML element, but the outerHTML gets also the tag and attributes representing that element.


- Example, adds the string with a HTML element in a textarea:
<div id='id1'>JavaScript
 <p>Example outerHTML - CoursesWeb.net/javascript</p>
</div>
<p>If you click on the following button, it adds in textarea the HTML element with id 'id1'.</p>
<button id='btn1'>Try it</button><br>
<textarea id='txta' cols='40' rows=4'></textarea>

<script>
var txta = document.getElementById('txta');

document.getElementById('btn1').addEventListener('click', (ev)=>{
 var cnt = document.getElementById('id1').outerHTML;
 txta.value = cnt;
});
</script>
2. Syntax, add /replace content with outerHTML:
element.outerHTML = 'HTML content';

The difference between outerHTML and innerHTML is that outerHTML replace the element itself with the new content.


- Example, replaces a Div with another HTML tag:
<div id='id1'>Div
 <div id='id2'>JavaScript</div>
</div>
<p>If you click on the following button, it replaces the the Div above with a H4 element.</p>
<button id='btn1'>Try it</button>

<script>
document.getElementById('btn1').addEventListener('click', (ev)=>{
 document.getElementById('id1').outerHTML ='<h4>Example outerHTML - https://coursesweb.net/javascript</h4>';
});
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
innerHTML and outerHTML to Get and Replace HTML content

Last accessed pages

  1. PHP Simple HTML DOM Parser (12452)
  2. Disable button and Enable it after specified time (17532)
  3. Get Mime Type of file or string content in PHP (6229)
  4. PHP MySQL - using MySQLi (9669)
  5. Integer and Float value in Select with PDO from string to numeric (8658)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)