0

I have placed one javascript (hide.js) in my html Page ... Hide function is not working .
My scripts are in right order
My code is as per tutorial

<!DOCTYPE html >
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery Example </title>
</head>
<body>
    <p id="paragraph ">
        This is Paragraph</p>
    <script type="text/javascript" src="http://localhost/WebApplication2/js/hide.js"></script>
    <script type="text/javascript" src="http://localhost/WebApplication2/js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="http://localhost/WebApplication2/js/toggle.js"></script>
</body>
</html>

hide.js

$('#paragraph').click(function () {
    $('#paragraph').hide();
});

I have test it with Firebug lite on Chrome .. Console doesnt show any error .. in script is also showing external scripts ...Other script are working fine
I have rearranged script Order like this

<script type="text/javascript" src="http://localhost/WebApplication2/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/toggle.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/hide.js"></script>

still it is not working .. i couldnt find any relevant link ..whats wrong ..Please Suggest

5
  • your hide.js would not be able to understand any of the selectors if you dont have a reference to the jquery library first, the answers below showld do the trick for you. Commented Apr 10, 2014 at 6:47
  • Did you check on firebug that the js files are loading ? Commented Apr 10, 2014 at 6:48
  • Also, remove the space from your p tag id. Commented Apr 10, 2014 at 6:51
  • i check in script section .. it shows scripts ... Commented Apr 10, 2014 at 6:53
  • OK got it ....Problem is in "<p id="paragraph ">" extra space ..as suggested by rooby ... thanks Commented Apr 10, 2014 at 6:55

6 Answers 6

2

Because, you included hide.js before jquery.js. You should include hide.js after jquery.js like so...

<script type="text/javascript" src="http://localhost/WebApplication2/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/toggle.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/hide.js"></script>

In case you must include hide.js first before jquery.js. Change your code to

$( document ).ready(function() {
 $('#paragraph').click(function () {
    $('#paragraph').hide();
 });
}

In this way, your script will work after whole page is loaded.

Or, similarly you can use.

$(function() {
  // Your Code
});
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, you beat me to it.
Yes. you don't need it if you include hide.js after jquery.js. In my answer, i just give you a choice.
1

order should be, include jquery main file all above.

<script type="text/javascript" src="http://localhost/WebApplication2/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/hide.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/toggle.js"></script>

core jQuery file must be include before any other plugin as they may use core jquery methods internally.

1 Comment

Although he says he has no errors in the console, which he would have in this case.
0

You have a space in the id of your paragraph tag, which is invalid, so your javascript cannot select that paragraph using that id.

You need to remove the space from your id attribute.

Comments

0

Order of script - hide.js is using jQuery so it has to be included after jquery.js

<script type="text/javascript" src="http://localhost/WebApplication2/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/toggle.js"></script>
<script type="text/javascript" src="http://localhost/WebApplication2/js/hide.js"></script>

Your browser console should show you an error saying something like Uncaught ReferenceError: $ is not defined

2 Comments

console is blank .. why it is blank ?
@user3487944 are you share the js files are downloaded... look at your network tab to see whether the js files are giving 200 OK status
0

your id has space.remove it and try again <p id="paragraph ">This is Paragraph</p>

it should be like this <p id="paragraph">This is Paragraph</p>

Comments

0

I have discovered a very strange thing which probably could be the cause. It should certainly help you.

In your code, I see you have only external JS files. After those external files, include an empty tag. It should work.

Cause is not known but external JS does not get included at times if you do not have inline or empty script tag. It will be very interesting to dig in more. Let me update once I figure out.

Environment: Chrome latest Version 44.0.2403.130 m

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.