2

I'm on CodePen, trying to practice using JS and jQuery in conjunction with HTML.

HTML:

<button>asdasads</button>

JS:

$('button').on('click', function() {
    alert('woot');
});

Link to the pen, which doesn't work.

It's meant to display an alert when the button is clicked. But it does nothing.

Here's another one, with the same HTML/JS. The only difference is that I edited this second one out of a "make this paragraph disappear by clicking a button" example CodePen pen.

This CodePen pen works 100%.

which works just beatifully. The code is identical. What is going on here? I cannot find anything on Google, there's no mention of having to configure CP in any way to make your JS mingle with your HTML.

Then again... As a child there was a room in my parent's house that was being renovated and the floor was gone, leaving a fall to the basement. I played a game of opening and closing a door to the room, pretending to be surprised that the floor was gone. Until one time I actually forgot and ran in (yes, I fell), and that was only like the 4th time I opened the door. So, yeah.

Thank you in advance. This is my first go at asking here. Be gentle! :E

2
  • You need to include jquery as an external library. Commented Oct 15, 2017 at 18:51
  • Thank you, snapjs, as well. This was correct. Commented Oct 15, 2017 at 18:55

2 Answers 2

4

enter image description here

You need to add the jQuery library to the Pen. Just click the gear in the JS block, then click Quick Add, like in this photo

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

1 Comment

Thank you. This is not only correct but instructive as well. I'd love to mark this as correct; however I promised to mark Indent's answer as the correct one.I asked him if I could choose this as correct instead. Shows that I shouldn't be so hasty... :)
1

You need add JQuery (Add External JavaScript)

https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js

3 Comments

Your answer was indeed correct, but Pato Salazar answered more thoroughly. I said I will mark your answer as correct, but you know what? I'm not sure what to do here. Stackoverflow is filled with comments about some other answer deserving to be marked the correct answer. While yours is correct, the one below is even better. Could I have your permission to mark the mentioned other answer as correct?
it is fine... don't worry about it @JaakkoPöntinen
It' s ok for me too. You can mark Pato Salazar answer.

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.