document.getElementById not displaying when using with external 'js' file

时间:2019-01-09 22:00:10

标签: javascript jquery

I am working on basics of javascript, here I have got a question? Could some one help me?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Javascript output</title>
</head>
<body>
    <p id = "abc" > Hello, I am here! </p>
    <button onclick = "myFun()" > Try it </button>   
    <script type="text/javascript" src ="sample.js"></script>
</body>
</html>

sample.js

function myFun(){
    document.getElementById('abc').innerHTML = 'hey there';
    document.write("Hello World");
}

On executing this, 'hey there' message from function myFun() is not getting displayed. I am expecting on clicking 'Try it' , it should display 'hey there' and then 'hello world' , but its just giving me 'hello world'.

However, if I just have document.getElementById('abc').innerHTML = 'hey there'; in my function myFun() its displaying 'hey there' message.

0 个答案:

没有答案