Adola
Adola

Reputation: 588

jquery .on() is not triggering

I have the following code:

console.log($(".resultLike"));
$(".resultLike").on("click", function (event) {
    alert('test');
    event.stopPropagation();
    alert('test1');
    value['clicked'] = 1;
    $.ajax({
        url: 'scripts/php/userProfile.php',
        data: {
            action: value
        },
        type: 'post',
        dataType: 'json',
        success: function (profileData) {
            if (profileData == 'removed') {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('Favourite?');
            } else if (profileData == 'notLoggedIn') {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('Login to add');
            } else {
                $(me).children('.resultInside')
                .children('.resultData')
                .children('.resultLike').html('un-Favourite');
            }
        }
    });
});

My expectations is that when you click on the div resultLike, then it will preform the function(). However, it does nothing. I have two alert() calls in there, and neither is being called. The output of console.log() is as follows:

[
<div class=​"resultLike searchBarGameLike">​</div>​
] 

That proves it's being put on the page. Any help would be appreciated, thanks.

EDIT:

I think it should be mentioned that I'm actually using two .on() events. This is actually all my code. The issue is around the $("body").on("click", ".resultLike", function(){ Line, it's not working.

$searchedGamesContainer.on(
    "click",
    ".box",
    function(event){
        if(!$displayLock){
            $displayLock = true;
            var description;
            var user_id;
            if($(this)[0].style.width == '75%'){
                var org_img = $(this).children(".resultInside").find("img").attr("src"); 
                $(this).children(".resultInside").append("<img src='"+org_img+"' id='imgId'/>");
                $(this).children(".resultInside").css('height','auto');
                $(this).css('width', '18%');
                $(this).css('height', 'auto');
                $(this).find(".resultData").fadeOut('fast');
                setTimeout(function(){$searchedGamesContainer.masonry('reload');},300);
                setTimeout(function(){$displayLock = false;},1000);
            }else{
                var me = this;
                var pos; 
                largeImage=  new Image();
                value['name']=$(me).find("p").html();
                oldImage = $(this).find("img").attr("src");
                for(var i = 0; i<$searchBarGames.length; i++){
                    if($searchBarGames[i][5] == value['name']){
                        pos = i; 
                        break
                    }
                }

                description = $searchBarGames[pos][2];
                $(me).find("img").hide();
                largeImage.src = $searchBarGames[pos][4];
                $(me).find("img").attr("src",largeImage.src);
                $(me).children(".resultInside").css('height','400px');
                $(me).css('width', '75%');

                $(me).children(".resultInside").html("\
                    <div class='resultData'>\
                        <div class='resultImg'><img src='"+ largeImage.src +"'></div>\
                        <div class='resultName'>" + value['name'] + "</div>\
                        <div class='resultDesc'>" + description +"</div>\
                        <div class='wikiLink searchBarGameWiki'><a href='http://wikipedia.org/wiki/" + value['name'] + "'>Wikipedia</a></div>\
                        <div class='resultLike searchBarGameLike'></div>\
                    </div>");
                value['clicked']=0;

                $.ajax({
                    url:'scripts/php/userProfile.php',
                    data:{action:value},
                    type: 'post',
                    dataType: 'json',
                    success:function(profileData){
                        //logic
                    }
                });     

                console.log($(".resultLike"));

                $("body").on("click", ".resultLike", function(){
                        alert('test');
                        event.stopPropagation();
                        alert('test1');
                        value['clicked']=1;
                        $.ajax({
                            url:'scripts/php/userProfile.php',
                            data:{action:value},
                            type: 'post',
                            dataType: 'json',
                            success:function(profileData){
                                //logic
                            }
                        });
                    }
                );  
            }
        }
    }
);

Upvotes: 0

Views: 127

Answers (2)

Adola
Adola

Reputation: 588

This problem actually ended up being completely because of CSS.

For the div element I wanted the alerts on, I had markup to look like a button.
However, I noticed this:

.resultLike:active {
    position:relative;
    top:1px;
    z-index:1235;
}

I'm not 100% why, but I think this was actually changing where the button was when clicking it. Meaning as soon as it was active, it wasn't where your mouse was. I could be wrong, but this fixed it.

Upvotes: 0

Yngve B-Nilsen
Yngve B-Nilsen

Reputation: 9676

try

$("body").on("click", ".resultLike", function(){
      // your code goes here.
});

Your implementation will likely not be bound if the div.resultLike is added dynamically

Upvotes: 3

Related Questions