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. :(

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.