Jump to content

Change Td Bgcolor On Jquery Ajax Callback With Checkbox


Wilf

Recommended Posts

Hi Guys,

I have a set of script to send data via checkbox toggle. After a checkbox is checked. A value is sent from PHP as 1. So I test the value within success callback. Everythings works fine but I can't make the background color change according to the given value from php. Please have give me a hand.

 

<script type="text/javascript">
     jQuery(function($){
   $('input[name=alm]').click(function(){
   var id=$(this).attr('id');
   var alm=$(this).val();
   $.ajax({
	    type:'GET',
	    url:'inc/villas_allotment_ajax_update.php',
           data:'id='+id+'&alm='+alm,
               /*success: function (result) {$('#bg').css("background-color", "#66cc00");alert(data);},
               error: function (result) {$('#bg').css("background-color", "#ff8080");},*/
               success: function(html) {
                   if(html=="1"){//change td bg to green if succeed
                       $('#bg').css("background-color", "#66cc00");            
                       alert(html);
                   }else{
                       $('#bg').css("background-color", "#ff8080");
                       alert(html);
                   }
                 }	     
           });
       });
   });
</script>

 

 

Here's my html:

<table>
[indent=1]<tr>[/indent]
[indent=2]<td id="2012-11-01-001" align="center"><input type="checkbox" id="2012-11-01-001" name="alm" value="1" checked /></td>[/indent]
[indent=2]<td id="2012-11-02-001" align="center"><input type="checkbox" id="2012-11-02-001" name="alm" value="1" checked /></td>[/indent]
[indent=2]<td id="2012-11-03-001" align="center"><input type="checkbox" id="2012-11-03-001" name="alm" value="1" checked /></td>[/indent]
[indent=2]...[/indent]
[indent=2]<td id="2012-11-30-001" align="center"><input type="checkbox" id="2012-11-30-001" name="alm" value="1" checked /></td>[/indent]
[indent=1]</tr>[/indent]
</table>

 

 

 

The questions is:

How can I change the td background-color after a callback is proved (html=="1") according to its td.id (not #bg as I wrote)

 

Please help me out, I've spent many hours to solve this including some samples from this website. :(

Maybe something like this would help

 

<script type="text/javascript">
   jQuery(function($){
       $('input[name=alm]').click(function(){
           var parentTD = $(this).parents('td');
           var id=$(this).attr('id');
           var alm=$(this).val();
           $.ajax({
               type:'GET',
               url:'inc/villas_allotment_ajax_update.php',
               data:'id='+id+'&alm='+alm,
               success: function(html) {
                   if(html=="1"){//change td bg to green if succeed
                       parentTD.css("background-color", "#66cc00");				    
                   }else{
                       parentTD.css("background-color", "#ff8080");
                   }
               }			  
           });
       });
   });
</script>

  • 4 weeks later...

Unless the lack of sleep has really got my head on sideways, you have a <td> element with id 2012-11-01-001 and a child element of said <td> with an id of 2012-11-01-001. To identify two separate elements with the same identifier, use class not id. ID refers to a single element with that unique identifier. I don't know enough about jQuery to comment on whether it will figure out what you did and compensate or if that will even cause you any problems, but what I do know is that is a bad practice and you should amend it. The parent TD is identified in your jQuery as the parent of the checkbox so it needs no actual unique identifier since it is uniquely identified as the parent of the checkbox.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.