Search code examples
javascriptexternal

Changing the value of paragraph using external js file code


I have created an html file

<html>

<head>
  <title>test</title>
</head>

<body>
  <p id="demo">l</p>
  <script type="text/javascript" src="profile/test.js">
    s(shiva);
  </script>
</body>

</html>

i want to change the value of the paragaraph. So my external js file is

function s(nam) {
  document.getElementById("demo").innerHTML = nam;
}

But is not changing can anyone suggest me how can change the value.


Solution

  • You can not include code in a javascript tag with a "src" attribute. So move the loose javascript in a new tag.

    <script type="text/javascript" src="profile/test.js"></script>
    
    <script type="text/javascript">
        s('shiva');
    </script>