stephen kronwith
stephen kronwith

Reputation: 87

Why no fadeout?

I know I must be missing a semicolon or something simple, but the following code is to fade out a paragraph, which is does, fade in a div called newtext and a button styled with btn2, which it does. However, when I click the button, the div newtext is supposed to fade out but doesn't. Any idea what's wrong in the code? Thanks.

$(document).ready(function(){

   setTimeout(function(){

       $("p").fadeOut();
       $("#newtext").fadeIn();

   $("btn2").click(function(){
       $("#newtext").fadeOut()
       });


   },2000);


});

Upvotes: 0

Views: 41

Answers (2)

qwertynl
qwertynl

Reputation: 3933

This code: $("btn2") implies that you have an html element like so: <btn2>...</btn2>

Perhaps you meant to select a class or id?

For example: $(".btn2") for a class or $("#btn2") for an id.

Upvotes: 1

Sergio
Sergio

Reputation: 28837

You are missing a class or ID selector here:

$("btn2")

Your selector should be $("#btn2") or $(".btn2"), if it is resp. a ID or a class in that element.

You might also want to move the click event attacher to outside your setTimeout. Like that it will only attach after 2 seconds. (maybe it is the behaviour you want).

Upvotes: 1

Related Questions