2nd button cannot execute jquery CSS -
i have 2 live events need change background color whether user click button on .padd or .paddc. first button working, background can change not second button. wrong code jquery?
<a id="hms" href="1" onclick="adduser('1'); return false;"><img id="t1" class="padd" src="tta/addr.png"></a> <a id="hms2" href="2" onclick="validateuser('2'); return false;"><img id="te2" class="paddc" src="tta/addr.png"></a> $(document).ready(function(){ var ffd = "0"; $(".padd").live('click',function(event){ //update profile - remove pending $.ajax({ type: "post", cache: "false", url: "pendingupd.php?pid="+event.target.id, success: function(aaa) { var ttf= "#t" + event.target.id; if(aaa=="approved"){ $(ttf).css("background","url('tta/pass.png') 50% 50px no-repeat"); $(ttf).css("background-color","#ffffda"); } else { $(ttf).css("background","none"); $(ttf).css("background-color","none"); } if(aaa=="error"){ $(ttf).css("background-color","#f0b7b7"); } } }); }); $(".paddc").live('click',function(event2){ //update profile - remove pending $.ajax({ type: "post", cache: "false", url: "pendingupd.php?pid="+event2.target.id, success: function(aaa2) { var ttf2= "#te" + event2.target.id; if(aaa2=="approved"){ $(ttf2).css("background","url('tta/pass.png') 50% 50px no-repeat"); $(ttf2).css("background-color","#ffffda"); } else { $(ttf2).css("background","none"); $(ttf2).css("background-color","none"); } if(aaa2=="error"){ $(ttf2).css("background-color","#f0b7b7"); } } }); }); });
the difference between 2 functions first function has var ttf= "#t" + event.target.id; while second has var ttf2= "#te" + event2.target.id; leads me believe issue in dom. sure has right id? supposed different?
either way, shorter version of code might this:
$(document).ready(function(){ var ffd = "0"; // jquery lets select more 1 element. // combining them makes intend more obvious. // , means create fewer functions. $(".padd, .paddc").live('click',function(event){ var ttf= $(this).hasclass( ".paddc" )? "#t": "#te" ; //update profile - remove pending $.ajax({ type: "post", cache: "false", url: "pendingupd.php?pid="+event.target.id, success: function(aaa) { // replace ttf = "#t" if te typo. updatependingstate( aaa, ttf + event.target.id ); } }); }); }); // there no point in having created // every time ajax call made (the old way). function updatependingstate( aaa, selector ) { // initialize values defaults. var bgcolor = "none", bgimg = "none"; if(aaa=="approved"){ // update if change merited. bgcolor = "url('tta/pass.png') 50% 50px no-repeat"; bgimg = "background-color","#ffffda"; } else if(aaa=="error"){ bgcolor = "#f0b7b7"; } // grab value once , use multiple times. why wast function calls? var ttf = $(selector); ttf.css("background",bgimg); ttf.css("background-color",bgcolor); }
Comments
Post a Comment