2

I got the following code on w3school :

<!DOCTYPE html>
<html>
<head>
<script>
function changetext(id)
{
id.innerHTML="Ooops!";
}
</script>
</head>
<body>
<h1 onclick="changetext(this)">Click on this text!</h1>
</body>
</html>

This is working well. Now for the sake of practice, I want want to do it by an external js file. My codes are as follows Html :

<!DOCTYPE html>
<html>
<body>

<p>Click the button to execute the <em>displayDate()</em> function.</p>

<button >Try it</button>


<p id="demo"></p>
<script src="function.js"></script>
</body>
</html> 

My js file:

function displayDate()
{
    document.getElementById("demo").innerHTML=Date();
}
onclick='displayDate()';

But above code is not working. What should I do for this.

3
  • YOu cant right like this - onclick='displayDate()'; in js file Commented Apr 19, 2014 at 9:15
  • Thanks.. What should I do for this.. Commented Apr 19, 2014 at 9:21
  • CHeckout answer given by @Maurice Commented Apr 19, 2014 at 10:47

2 Answers 2

5

This is often done with an ID on the button. To ensure the DOM is loaded when the script is executed, it is done in a load handler.

html:

<!DOCTYPE html>
<html>
<head>
    <script src="function.js"></script>
</head>
<body>

   <p>Click the button to execute the <em>displayDate()</em> function.</p>

   <button id="myButton">Try it</button>

   <p id="demo"></p>

</body>
</html>

function.js:

function displayDate()
{
    document.getElementById("demo").innerHTML=Date();
}

window.onload = function() {
    var btn = document.getElementById("myButton");
    btn.onclick = displayDate;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Is it necessary to use window.onload ?
Yes it is, because if you set the handler directly in the script, the markup may be incomplete, and the button may not exist. But I just noticed the jquery tag in your question; if you're using jquery, it can be a little simpler.
I am very new to Javascript and do not know anything about jquery. Should I try it after studying jquery ? I am editing tags.
My opinion is that you should start with learning Javascript, then learn jquery.
Yes sir, I am doing the same. But for the above problem ,should I take the help of jquery?
|
1
<!DOCTYPE html>
<html>
<body>

<p>Click the button to execute the <em>displayDate()</em> function.</p>

<button id="try it" onclick='displayDate()' >Try it</button>


<p id="demo"></p>
<script src="function.js"></script>
</body>
</html>

js file

    function displayDate()
{
    document.getElementById("demo").innerHTML=Date();
}

2 Comments

Just out of curiosity, why is this answer being downvoted? Is it because the click event shouldn't be in the HTML? Because the code seems to work just fine.
I can't understand why the answer is downvoted either !! anyway I hope it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.