0

I'm trying to set a checkbox to checked from an if statement but it just doesn't seem to want to work...

This is the HTML:

<input type="checkbox" id="aircon" name="aircon" onchange="QuoteRefresh();" class="toggle"  />

and this is within my .js file:

$('#aircon').prop('checked', true);

I can't see any errors in the console.

Can anyone shed any light on this.

It's within a AJAX success response:

if (SelectedLevel == "Gold") {


  $('#aircon').prop('checked', true);

}

Can anyone help me??

This is what I'm getting within the console:

[input#aircon.toggle, context: document, selector: "#aircon", constructor: function, init:      function, selector: ""…]
0: input#aircon.toggle
context: document
length: 1
selector: "#aircon"
__proto__: Object[0]

1 Answers1

0

The code you posted should work, maybe your QuoteRefresh(); method is returning false, or maybe your code handling the checked propertery isn't attached to the appropriate event that you want.

Here's a JSFiddle of the prop method working.

edit (with information gathered from your initial comment on this answer)

If I'm understanding right, you're using the bootstrap toggle buttons component (which is represented with radio button values), and you're trying to change the actively toggled button with .prop('checked', true); but your components visual style isn't changing. This is because changing the checked property on a radio button from true to false or vice versa doesn't trigger any javascript events, which bootstrap relies on in order to update the visual state of the radio buttons.

Community
  • 1
  • 1
Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54
  • I've just taken it out of the IF statement and it just doesn't seem to work... Could it be becuase I have styled the radio button into a toggle... – user2438464 Jul 12 '13 at 07:50
  • This is because of 'bootstrap-toggle-buttons' I've just added a fault on github. – user2438464 Jul 12 '13 at 08:37
  • I edited my answer to reflect what I think your problem really is - You should consider editing your question to be more specific as to your problem. – Jim Rubenstein Jul 12 '13 at 12:41