Generah Ben
Generah Ben

Reputation: 15

Jquery addClass Not working

I don't really know what I'm doing wrong. I have tried severally and it's still not working. Here is my code.

I want <p>content</P> content to disappear after 3seconds

$loginmessage = "<br /><p class='alert alert-danger'>Login Fail</p>";
<div id="alertarea" class="text-center"><?php if(isset($loginmessage)) echo $loginmessage ?></div>

$("document").ready(function(){
    function hideWithTime(){
        if($("div#alertarea p").html != ""){
            $(this).addClass('hideit');
        }
    }
    // setTimeout(hideWithTime(), 2000);
    hideWithTime();
});

Upvotes: 0

Views: 64

Answers (3)

Generah Ben
Generah Ben

Reputation: 15

Thank you for your support.. This worked with your suggestion

setTimeout (function(){ if($("div#alertarea").html() != ""){ $("div#alertarea>p").addClass('hideit'); } }, 5000);

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

Its html() - a function not html - a property

Cache your element, this doesn't refer to $("div#alertarea > p")

$("document").ready(function(){
       var element = $("div#alertarea > p");
        function hideWithTime(){
            if(element.html() != ""){
               element.addClass('hideit');//ps bootstrap has a hide class
            }
        }

         setTimeout(hideWithTime, 2000); 
    });`

Upvotes: 2

A. Cancela
A. Cancela

Reputation: 45

If the child of the div is just a p tag, your code finds always no html. better do:

function hideWithTime(){
    if($("div#alertarea").html() != ""){
            $("div#alertarea>p").addClass('hideit');
        }
    }

Upvotes: 0

Related Questions