Skip to content
Advertisement

JavaScript window.onload

JavaScript

In this little example, I want to save the initial innerHTML of the h1 tag (id=title) to a variable after loading the page for the first time.

And then instead of the document.getElementById('title').innerHTML = 'Welcome to JavaScript' which is inside the if statement, I want to substitute that above mentioned variable with the phrase “Welcome to JavaScript“.

My main intention is, wwhenever some one leaves the textbox (id=myText) blank and click on the submit button, script should replace the innerHTML of the h1 tag (id=title) to the initial value that was there in the first page load and pop out that alert box. (Maybe user has changed the innerHTML of the h1 before, but the script should replace it to the initial value that was there in the first page load).

Advertisement

Answer

You can declare a hidden input.

JavaScript

and populate the hidden field value via body onload JS function like.

JavaScript

And in changeTitle() function rewrite if block as.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement