2

I would very much like to be able to toggle having an x and not having an x in

<td class="checkbox"> <input name="signed" type="checkbox" checked /> x </td>

See JSFiddle for example

http://jsfiddle.net/littlesandra88/GMSCW/

The code that is triggered when Submit is pressed is

$('form').live('submit', function(){
   $.post($(this).attr('action'), $(this).serialize(), function(response){
         // do something here on success
   },'json');
   return false;
});

I assume I have to something in the lines of

$(this).getElementByClass("checkbox").td.toggle('x');

But checked should still be updated in the <input name="signed" type="checkbox" checked />

Does anyone know how to do this?

Sandra Schlichting
  • 25,050
  • 33
  • 110
  • 162

5 Answers5

1

Wrap the X in a span and then hide that

<td class="checkbox"> 
      <input name="signed" type="checkbox" checked />
      <span id='toggleme'>x</span>
 </td>

 //change the visibility of the x
 $('#toggleme').toggle();

// check the checkbox
$('input[name=signed]').attr('checked', true);
JohnFx
  • 34,542
  • 18
  • 104
  • 162
1

To check a checkbox in jQuery use you ELEMENT.attr("checked","checked") or the jQuery 1.6 way with ELEMENT.prop("checked", true)

dotty
  • 40,405
  • 66
  • 150
  • 195
1

The best idea, IMO, is to put a <label> right next to the checkbox and then toggle it with $("input[type=checkbox] + label", this).toggle().

To change the checkbox, the examples other users gave work just fine.

http://jsfiddle.net/w5nAr/

This assumes you don't want to change the checkbox, just the text next to it. If you want both, here's another example:

http://jsfiddle.net/w5nAr/4/

brymck
  • 7,555
  • 28
  • 31
1

Try looking at the code here it's based off the work by Bryan.

dotty
  • 40,405
  • 66
  • 150
  • 195
0
$({some identifier for your checkbox}).attr("checked",true)
El Guapo
  • 5,581
  • 7
  • 54
  • 82
  • 3
    Not everyone is on 1.6, so `attr()` is a better solution at the moment. But in the future we should defiantly use `prop()` – dotty Jun 10 '11 at 14:16
  • Here are some great answers on [how `prop()` and `attr()` differ](http://stackoverflow.com/questions/5874652/prop-vs-attr). – Town Jun 10 '11 at 14:28