this - jQuery methods on dynamic elements -


unable use addclass method nor save instances of dom elements $(this) on dynamically loaded elements. using .on method click events on these elements not able manipulate them.

$(document).on("click",".playpause",function(){             if($(this).attr('src') == 'img/play.png'){                 $(this).attr('src','img/pause.png');                 var songid = $(this).parent().siblings('.top_container').children('input').val();                 $.post('songs.php',{songid : songid}, function(path){                     if(globalsong.playstate && !($(this).hasclass('prevselection'))){                         $('.prevselection').attr('src','img/play.png');                         globalsong.pause();                         $('.prevselection').removeclass('prevselection');                     }                     globalsong = soundmanager.createsound({                         id: ("sound" + songid),                         url: (songspath + path),                         volume: userprefvolume                     });                     globalsong.play();                     $(this).addclass('prevselection');                 });             } else {                 $(this).attr('src','img/play.png');                 globalsong.pause();             }         }); 

you need save $(this) after function()

$(document).on("click",".playpause",function(){              var $this = $(this); //////save here//////              if($(this).attr('src') == 'img/play.png'){                 $(this).attr('src','img/pause.png');                 var songid = $(this).parent().siblings('.top_container').children('input').val();                 $.post('songs.php',{songid : songid}, function(path){                  //////////use $this instead of $(this) inside here///////////                     if(globalsong.playstate && !($(this).hasclass('prevselection'))){//$this                         $('.prevselection').attr('src','img/play.png');                         globalsong.pause();                         $('.prevselection').removeclass('prevselection');                     }                     globalsong = soundmanager.createsound({                         id: ("sound" + songid),                         url: (songspath + path),                         volume: userprefvolume                     });                     globalsong.play();                     $(this).addclass('prevselection'); //$this                 });             } else {                 $(this).attr('src','img/play.png');                 globalsong.pause();             }         }); 

use $this instead of $(this) inside $.post() function


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -