1

I need to toggle a checkbox-button in Jquery. assuming they are originally checkboxes, I tried

$("btn").attr('checked',true);

but did not function when the using checkbox as a button.

simply, I just want is to set the button below..

http://jqueryui.com/demos/button/#checkbox

like the checkbox is checked in example:

http://www.electrictoolbox.com/check-uncheck-checkbox-jquery/

--edit-- see the sample code..

<html><head>
<link type="text/css" href="css/smoothness/jquery-ui-1.8.13.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.13.custom.min.js"></script>
<script>
$(function() {
$( "#check" ).button();//when not a button it works .. just uncomment it.
$( "#check" ).attr("checked",true);//??
});
</script></head><body>
<input type="checkbox" id="check" /><label for="check">Toggle</label>
</body></html>
mayur dange
  • 21
  • 1
  • 4

5 Answers5

1

$("btn").attr('checked','checked');

Also see Setting "checked" for a checkbox with jQuery?

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

You need to specify class or id... e.g: $("#btn")

Blazes
  • 4,721
  • 2
  • 22
  • 29
0

you missed the dot from selector :P

  $(".btn").attr('checked',true);
Treemonkey
  • 2,133
  • 11
  • 24
0

try this...

$(".btn").attr('checked',true); 

As i can see you are missing . or #, depends upon whether btn is id or class

Vivek
  • 10,978
  • 14
  • 48
  • 66
0

sorry, that's a bug in jquery; http://bugs.jqueryui.com/ticket/7476

mayur
  • 1