0

I have a conditional that must check or uncheck an input checkbox, but it doesn't work, and I don't know what to do. I have searched and tried some jquery checkbox manipulation options, but any of them work. With a console log messages I know that the conditional works and the input checkbox is accessible. Please, give me a hand.

if(parseInt(permiso_acceso)==1) {
   var valor = $('#permiso_acceso').val();
   console.log('Must be checked '+valor);//It works
   $('#permiso_acceso').attr('checked',true);
} else {
   var valor = $('#permiso_acceso').val();
   console.log('Must be unchecked '+valor);//It works
   $('#permiso_acceso').attr('checked',false);

}

FranQ
  • 47
  • 1
  • 7

5 Answers5

1

Try this

jQuery 1.6+

Use .prop() function:

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

if you want to check if a checkbox is checked or not:

$('form #mycheckbox').is(':checked');
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
1

In place of .attr, use .prop jquery method (included in jquery1.6).Here is detailed discussion link.

if(parseInt(permiso_acceso)==1) {
   var valor = $('#permiso_acceso').val();
   console.log('Must be checked '+valor);//It works
   $('#permiso_acceso').prop('checked', true);
} else {
   var valor = $('#permiso_acceso').val();
   console.log('Must be unchecked '+valor);//It works
   $('#permiso_acceso').prop('checked', false);
}

Try This in jsfiddle

Community
  • 1
  • 1
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
0

val() will always return the value property of a radio or checkbox element, regardless of whether it is ticked or not. The logic of your code is correct.

I would assume the issue is due to the value of permiso_acceso not being set to the selected checkbox value.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

It happens, because attribute "checked" requeries value "checked", not true or false

Dmitry Seleznev
  • 955
  • 7
  • 7
  • This is incorrect. The `checked` attribute requires no one specific value. The presence of the keyword of the keyword is all that is required, however a value should be assigned for semantic reasons. Whether the value is `checked` or `true` or `foobar` is entirely up the developer. – Rory McCrossan Nov 14 '13 at 12:18
0

From my understanding you want to check and uncheck a checkbox based on a condition. check out this fiddle

var permiso_acceso = 1;
if(permiso_acceso==1) {
   var valor = $('#permiso_acceso').val();
   console.log('Must be checked '+valor);//It works
   $('#permiso_acceso').attr('checked',true);
} else {
   var valor = $('#permiso_acceso').val();
   console.log('Must be unchecked '+valor);//It works
   $('#permiso_acceso').attr('checked',false);
}

http://jsfiddle.net/Zhrt3/

try switching the variable permiso_acceso to 1 and 0.

its working

pallav_125
  • 211
  • 1
  • 2
  • 8
  • The conditional was working well. The log message was right. My problem was the method .attr(), once used .prop() all worked like a charm – FranQ Nov 14 '13 at 18:07