In first part we discussed about some facts about JS. Let’s go ahead….
- Writing into an alert box, using window.alert()
try this code and you can see an alert box.
- Writing into the HTML output using document.write().
The document.write() method should be used only for testing. Because If we’ll use document.write() after an HTML document is fully loaded, will delete all existing HTML.
below example will explain it. Try this code and you can understand what I’ve told above.
- Writing into an HTML element, using innerHTML.
By using document.getElementById(id) method, we can access an HTML element.
The id attribute defines the HTML element. The innerHTML property defines the HTML content:
When we click the “click” button, document.getElementById() method runs and It passes the “Ohh this is cool” string to header which id = “ID”.
- Writing into the browser console, using console.log().
In browser we can use the console.log() method to display data. For this we have to activate the browser console with F12 and select console in the menue.
First run the html file. Then press F12 and you can see a window. then click a console menue and you can see the answer 11.
let’s discuss about more things.