javascript - Unable to set element proprieties passed as argument -
i have following code (read comments , code):
<!-- panel --> <div class="panel panel-default" num="1"> <!-- there boring stuffs --> <div style='float: right;'> <!-- 3 buttons: hearth, tic , cross --> <span class="glyphicon glyphicon-heart-empty" aria-hidden="true" style="margin-right: 5px;" act="1"></span> <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true" style="margin-right: 5px;" act="3"></span> <span class="glyphicon glyphicon-ok" aria-hidden="true" style="margin-right: 5px;" act="0"></span> </div> <!-- other boring stuffs --> <script> $('.glyphicon').click(function(){ //panel father of button clicked panel = $(this).closest('div.panel'); //action: 1 like, 2 dislike, 3 signal, 0 accept act = $(this).attr('act'); //send request file change in database. ignore until change() function call $.post( 'action.php', {id : panel.attr('num'), act : act}, function(data) { if (data == '1') { //if request correct change dom elements //first father panel, second button clicked (this problematic argument), , action (1, 2, 3, 0) change(panel, $(this), act); } else { alert('error occurred...please try again!'); } } ); }); //change dom elements function change(panel, obj, act) { //switch action switch(parseint(act)) { case 1: //this code executed 3 action doesn't work. nothing change. think problem var obj obj.removeclass('glyphicon-heart-empty'); obj.addclass('glyphicon-heart'); obj.attr('act', '2'); break; case 2: //same thing here, haven't checked because first code of case 1 need work. obj.removeclass('glyphicon-heart'); obj.addclass('glyphicon-heart-empty'); obj.attr('act', '1'); break; case 3: //the panel var works , blocks works panel.removeclass('panel-default'); panel.addclass('panel-warning'); break; default: panel.removeclass('panel-warning'); panel.addclass('panel-default'); } } </script>
i want when click hearth fills , if click again return empty. problem when try set element proprities of obj
element hearth clicked. i'm sure code executed because debugged adding console.log()
in cases 1 , 2. doesn't return errors , code seems work nothing happend , clicking again attr act
same.
inside $.post
success callback, this
no longer element clicking (which need pass change
). need save reference said element outside $.post
callback, , use parameter when calling change, such as:
$('.glyphicon').click(function(){ var icon = $(this), panel = icon.closest('div.panel'), act = icon.attr('act'); $.post( 'action.php', {id : panel.attr('num'), act : act}, function(data) { if (data == '1') { change(panel, icon, act); } else { alert('error occurred...please try again!'); } } ); });
Comments
Post a Comment