0

I wrote JavaScript in separate file now i want to use a function written in that .js file

HTML

<html lang="en">
<head>
    <title>try</title>
</head>
<body onload="hide()" > 
    <div id="final">
        <p> Hello World</p>
    </div>
    <script src="js/userFunction.js"></script>
</body> 
</html>

JS File

function hide(){
  $("#final").hide();
}

its not working, please suggest

1
  • 1
    $ is not a built-in JavaScript or DOM function. You also have to include whatever script provides that function (presumably jQuery). If you indeed intend to use jQuery, please follow the jQuery tutorial. It includes everything you need to know to get started. Commented Mar 10, 2014 at 8:14

3 Answers 3

2

You have not included the jQuery file

<html lang="en">
<head>
    <title>try</title>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script>
     function hide(){
     $("#final").hide();
    }
    </script>
</head>
<body onload="hide()" > 
    <div id="final">
        <p> Hello World</p>
    </div>
    <script src="js/userFunction.js"></script>
</body> 
</html>

jQuery is only an online reference to the jquery file. You may download the js file, keep it locally in your project folder and refer it from there.

Or

If the function is in "js/userFunction.js", make sure the path is correct, and if the path is correct and still not working try loading the script file in the head tag. The script file needs to be loaded first before the functions in it are actually called.

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

10 Comments

Assuming the OP wanted to use jQuery.
Are you talking to me? I didn't accept anything because I didn't ask the question.
No i thought this would notify the guy who posted the question .. I asked this to the user who posted the question.
You have to comment on the question then.
yes i have included <script src="code.jquery.com/jquery-1.11.0.min.js"></script>.
|
1

This won't work because you have not added the jquery library there in your page so in your console there will be an error stating that $ is not defined:

so add the jquery library before the script:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

or you can do with native js too:

function hide(){
  document.getElementById("final").style.display = 'none';
}

and instead of inline js try unobtrusive:

if jquery used:

$(function(){
    hide(); // <----call your hide func here
});

with native js:

window.onload = function(){ 
    hide(); // <----call your hide func here
};

Comments

0

Here is a pure javascript solution. http://jsfiddle.net/6Wems/

HTML

<div id="final">
    <p> Hello World</p>
</div>

Put this in head of your HTML if you do not want to use jquery:

window.onload=function(){
  document.getElementById("final").style.display = 'none';
}

3 Comments

Why are you suggesting this? What's the problem with the OP's code? Don't just post code, also provide an explanation. Otherwise, how is the OP supposed to learn?
@FelixKling Is it neccassery to use Jquery if not needed? If he will lean javascript i recommend the first steps without jquery.
Of course it's not necessary. But just posting code without an explanation is not really helpful. I mean, how would you feel if you go to store and ask customer service for help and they would just put a device in your hand and walk away...

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.