1

I have a simple script that works when included in the html body. However, when running this from a relative path, and including the

<script type="text/javascript" src="simple.js"></script>

in either the header or bottom of the html file, the script will no longer run.

Can anyone point as to what I am missing ?

Thank you.

Script simple.js:

<script>
        function firstFunction(){

            var a = document.getElementById("firstInput").value;

            if(! a){
                alert("minimum input of 1 char required");
            }else{
                document.getElementById("firstOutput").innerHTML=a;
            }


        }

        function secondFunction(){
            var b = document.getElementById("secondInput").value;

            if(!b){
                alert("minimum input of 1 char required");
            }


            document.getElementById("secondOutput").innerHTML=b;
            //document.getElementById("secondOutput").innerHTML = "updated!";
        }

1
  • 2
    just remove <script> from simple.js Commented Feb 8, 2016 at 19:03

1 Answer 1

3

You only need the <script> tag if you include the javascript into your html file.

In a .js file, it's a syntax error. Just write your javascript code without the tag!

File simple.js:

function firstFunction(){

        var a = document.getElementById("firstInput").value;

        if(!a){
            alert("minimum input of 1 char required");
        }else{
            document.getElementById("firstOutput").innerHTML=a;
        }


    }

    function secondFunction(){
        var b = document.getElementById("secondInput").value;

        if(!b){
            alert("minimum input of 1 char required");
        }


        document.getElementById("secondOutput").innerHTML=b;
        //document.getElementById("secondOutput").innerHTML = "updated!";
    }

As your file is right now, make sure to place the script right before the closing body tag

<script type="text/javascript" src="simple.js"></script>
</body>

so that the elements can be found when the script is running.

Sign up to request clarification or add additional context in comments.

Comments

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.