60

I need remove the attribute "checked" of one checkbox when errors occur.

The .removeAttr function not work. Any idea? :/

HTML

<div data-role="controlgroup" data-type="horizontal" data-mini="true" style="margin-left: auto; margin-right: auto; width: 100%; text-align: center;">
    <input type="checkbox" id="captureImage" name="add_image" class="custom" />
    <label for="captureImage" data-icon="checkbox">Image</label>
    <input type="checkbox" id="captureAudio" name="add_audio" class="custom" />
    <label for="captureAudio" data-icon="checkbox">Audio</label>
    <input type="checkbox" id="captureVideo" name="add_video" class="custom" />
    <label for="captureVideo" data-icon="checkbox">Video</label>
</div>

Javascript

$("#captureImage").live("change", function() {
    // $("#captureImage").prop('checked', false); // Here Work

    if($("#captureImage:checked").val() !== undefined) {
            navigator.device.capture.captureImage(function(mediaFiles) {
            console.log("works");
        }, function(exception) {
            $("#captureImage").prop('checked', false); // Not Works Here
            _callback.error(exception);
        }, {limit: 1});
    }
});

/*
$("#captureAudio").live("change", function() {
    if($("#captureAudio:checked").val() !== undefined) {
            navigator.device.capture.captureAudio(function(mediaFiles) {
            console.log("audio");
        }, function() {
            $("#captureAudio").removeAttr('checked');
            _callback.error;
        }, {limit: 1});
    }
});

$("#captureVideo").live("change", function() {
    if($("#captureVideo:checked").val() !== undefined) {
            navigator.device.capture.captureVideo(function(mediaFiles) {
            console.log("video");
        }, function(exception) {
            $("#captureVideo").prop('checked', false);
            _callback.error(exception);
        }, {limit: 1});
    }
});
*/
itsazzad
  • 6,868
  • 7
  • 69
  • 89
FabianoLothor
  • 2,752
  • 4
  • 25
  • 39

10 Answers10

111

Try...

$("#captureAudio").prop('checked', false); 
alex
  • 479,566
  • 201
  • 878
  • 984
72

Both of these should work:

$("#captureImage").prop('checked', false);

AND/OR

$("#captureImage").removeAttr('checked');

... you can try both together.

itsazzad
  • 6,868
  • 7
  • 69
  • 89
  • Only the second option worked for me but I dont know why. Is it because I set the checked attribute with the attr method, like: radio.attr('checked', 'checked')? – Erik Čerpnjak Jan 21 '16 at 09:13
  • @ErikČerpnjak yes then – itsazzad Jan 21 '16 at 16:22
  • 7
    **NOTE** per [.prop('checked',false) or .removeAttr('checked')?](https://stackoverflow.com/q/6169826/1366033), that **`.prop( "checked", false )`** is always preferable to `.removeAttr( "checked" )` – KyleMit Nov 29 '17 at 18:43
9

Try this to check

$('#captureImage').attr("checked",true).checkboxradio("refresh");

and uncheck

$('#captureImage').attr("checked",false).checkboxradio("refresh");  
A. Magalhães
  • 1,511
  • 2
  • 22
  • 31
5

I use prop attribute for unchecked the checkbox when errors occur. You don't need to use remove property for unchecked your checkbox.

$('input#IDName').prop('checked', false);

It is working fine for me. Hope it will work for you also.

Love Pandey
  • 330
  • 2
  • 9
3

Try:

$("#captureAudio")[0].checked = false;
macool
  • 659
  • 6
  • 11
  • I guess you are doing wrong the condition. Try: `if ( $("#captureImage")[0].checked )`. Avoid using those jQuery pseudo-selectors (`:checked`). – macool Nov 26 '12 at 02:37
  • @FabianoLothor also avoid raising Exceptions. Even something like `console.error` may crash your code. (It has happened to me) – macool Nov 26 '12 at 02:44
  • the condition is working properly. The problem is I can not run JQuery calls within the error function. – FabianoLothor Nov 26 '12 at 02:51
  • @FabianoLothor then, cache the variable. `captureImage = $("#captureImage")` (do it after the call to navigator). Then, in the callback function, just do: `captureImage[0].checked = false` – macool Nov 26 '12 at 02:58
3

using .removeAttr() on a boolean attribute such as checked, selected, or readonly would also set the corresponding named property to false.

Hence removed this checked attribute

$("#IdName option:checked").removeAttr("checked");
Machavity
  • 30,841
  • 27
  • 92
  • 100
3

In case someone's looking for an answer using pure JavaScript:

const audioLabel = document.getElementById('captureAudio');

audioLabel.removeAttribute('checked');
Davi Mello
  • 349
  • 4
  • 6
2

You could try $(this):

$("#captureAudio").live("change", function() {
    if($(this).val() !== undefined) { /* IF THIS VALUE IS NOT UNDEFINED */
            navigator.device.capture.captureAudio(function(mediaFiles) {
            console.log("audio");
        }, function() {
            $(this).removeAttr('checked'); /* REMOVE checked ATTRIBUTE; */
            /* YOU CAN USE `$(this).prop("checked", false);` ALSO */
            _callback.error;
        }, {limit: 1});
    }
});
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
0

try something like this FIDDLE

    try
      {
        navigator.device.capture.captureImage(function(mediaFiles) {
        console.log("works");
         });
      }

    catch(err)
      {
        alert('hi');
        $("#captureImage").prop('checked', false);

      }
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
-7

Sorry, I solved my problem with the code above:

$("#captureImage").live("change", function() {
    if($("#captureImage:checked").val() !== undefined) {
        navigator.device.capture.captureImage(function(mediaFiles) {
            console.log("works");
        }, function(exception) {
            $("#captureImage").removeAttr('checked').checkboxradio('refresh');
            _callback.error(exception);
        }, {});
    }
});
FabianoLothor
  • 2,752
  • 4
  • 25
  • 39