user3242861
user3242861

Reputation: 1929

I want to remove an element but the click () does not work

I want to click .close to remove .status-profile but click() does not work. I think the problem may be a class close to be within the class status-profile.

my html:

<div class="status-profile">
<div class="message">
    <span>You should complete your <b>profile.</b></span>
</div>
<div class="close">
    <i class="icon-cancel"></i>
</div>
<div id="bar" class="progress">
    <div class="progress-bar" role="progressbar" aria-valuenow="{res}" aria-valuemin="0" aria-valuemax="100" style="width: {res}%">
    </div>
</div>

my js:

$('.status-profile .close').click(function()
    {
        console.log("close");
        status = 100;
        $(".status-profile").remove();
    });

thanks to all

Problem solved: the problem was that the html is loaded trough ajax call and the class close was not yet in the DOM ... thanks

Upvotes: 0

Views: 79

Answers (3)

speti43
speti43

Reputation: 3046

You have to subscribe to click event in document.ready:

$(function(){
    $('.status-profile .close').click(function(){
        console.log("close");
        status = 100;
        $(".status-profile").remove();
    });  
});

Click on "Close":

JSFiddle

Upvotes: 1

microbingbing
microbingbing

Reputation: 51

when the js code execute the dom doesnt prepare and $('.status-profile div.close') cant find and element $(document).ready(function(){ dosometh })

Upvotes: 0

Jon
Jon

Reputation: 309

Is that the exact HTML that you're using? If so, the

<div class="status-profile"> 

is missing its closing tag which would prevent jQuery from working as you'd expect.

Add another

</div>

right at the end and see if that helps.

Upvotes: 0

Related Questions