2

Can someone tell me why I cannot add the handler separately and instead I have to do something like below to make it work ?

My question is why I should add the code for hiding the paragraph inside the $(document).ready() to make it work.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    alert('test');
});
</script>
<script>
$("p").click(function(){
        $(this).hide();
    });
    </script>
</head>
<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>

Working code-

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").click(function(){
        $(this).hide();
    });
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

</body>
</html>
3
  • Pretty sure it's because the DOM isn't loaded at <head> Commented Feb 28, 2017 at 4:09
  • 1
    In first code snippet your click event is not defined in document.ready function Commented Feb 28, 2017 at 4:09
  • 2
    There's another option that will work: put the JS at the end of the body, after the elements it tries to reference. Commented Feb 28, 2017 at 5:03

4 Answers 4

3

The problem in the first snippet is jQuery code registered before the document is loaded so it will not going to recognize any HTML element. And hence it will not work.

Solution:-

1.Either put your code at the bottom of the page (in case of first one).

2.Or wrap your code inside ($(document).ready(function(){..});) which you have already done in second code.

In both above cases HTML element are loaded properly and registered before your jQuery code and hence your code will start working

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

Comments

1

$(documet).ready(function(){}) executes only after all DOM elements parsed in page irrespective of location. When you write code without ready function then code executes sequentially. If javascript code is placed before DOM element appearance it will not work. If you place(1st case) -

<script>
$("p").click(function(){
    $(this).hide();
});
</script>

Just before body tag, it will work as expected - to be sure all DOM elements parsed in page make use of $(documet).ready(function(){}).

Comments

1

Change it as following..

$(document).ready(function(){
    alert('test');
    $("p").click(function(){
        $(this).hide();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

3 Comments

Isn't that what the OP is already doing in their second example? They're asking why the first way fails and the second way works.
I gave him code. developer can understand the difference.
"My question is why I should add the code for hiding the paragraph inside the $(document).ready() to make it work." - You didn't even attempt to answer the question, and the code you showed seems the same as what the OP said he already had that worked, but with an added alert.
1

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to the 'click' handler.

You could implement it in following ways:

  1. Include the script at the end of the body of the HTML which is always the best practice.

  2. Wrap your handling code into a function and call that function inside document.ready() or body.onload(). document.ready() is preferred as it does not wait for the resources like images and attach the events right after the HTML content is loaded.

  3. Use delegated events to attach event handlers. Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

I would do it like this:

(function(){
  $('p').on('click',function(){
    $(this).hide();
  });
})();
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    alert('test');
});
</script>
</head>
<body>

<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>

<script src=""></script>
</body>
</html>

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.