Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

on() with click event does nothing

Tags:

jquery

I have a div with an id of "close" inside a dynamically created div with an id of "box". The following code is meant to do something whenever the user clicks on close.

$('#box').on('click','#close',function(){
    alert(1); // Test to see if the click works
});

I'm using the Big Cartel CMS and if I click close in the "live preview mode", it seems to work fine, but whenever I actually publish the site and view it normally, it does absolutely nothing - no errors - nada.

Markup & CSS, just in case:

<div id="box"> <!-- Dynamically loaded -->
    <div id="close"></div>
    <h2 id="name"></h2>
    <div id="description">
        <p>blah...</p>
    </div>
</div>

#close{
    background: url(image-path);
    float: right;
    position: relative;
    top: 0;
    margin: 0 0 0 12px;
    width: 25px;
    height: 25px;
    cursor: pointer;
    z-index: 100;
}

What am I missing?

like image 240
verism Avatar asked Dec 16 '22 16:12

verism


1 Answers

The problem is because #box is also dynamic. You need the primary selector to be an element which is available when the page is loaded.

A primary candidate would be the element in to which you are loading #box.

like image 143
Rory McCrossan Avatar answered Dec 18 '22 04:12

Rory McCrossan