19

I'm using a material design lite checkbox and I'm trying to check or uncheck the element using JavaScript. I tried this:

document.getElementById("checkbox-1").checked = true;

That do not work. I tried the same approach with jQuery:

$("#checkbox-1").prop('checked',true); 

That did not work either. Any help would be appreciated.

onebree
  • 1,853
  • 1
  • 17
  • 44

8 Answers8

16

Currently, this component in 1.0.0 has a bug where it is not exposed as a widget. This has been fixed. Currently in master and in a 1.0.1 patch in a few days, it will be available to everyone in a stable build.

This is the proper method to handle it that will work if you have a patched version:

To check the element: document.querySelector('.mdl-js-checkbox').MaterialCheckbox.check()

And to uncheck: document.querySelector('.mdl-js-checkbox').MaterialCheckbox.uncheck()

The full API can be discovered currently by looking at the Source code and viewing the properties that don't end in an underscore. If they end in an underscore, they are for internal use only and external use is not supported.

We are working on getting the JS API's documented, but that will take some more time to finish and roll out to the site.

Finlay Percy
  • 6,763
  • 4
  • 22
  • 28
Garbee
  • 10,581
  • 5
  • 38
  • 41
10

For those of us (everybody) that have an id or even a class on the <input type="checkbox"> element vs the parent <label>, the trick is to run the MDL method on the parent. Otherwise you get an .MaterialCheckbox is undefined error.

.parentElement.MaterialCheckbox.check();
.parentElement.MaterialCheckbox.uncheck();

So, for example:

var myCheckbox = document.getElementById('my-checkbox');
myCheckbox.parentElement.MaterialCheckbox.check();
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
4

If you're using jQuery:

$('.mdl-js-checkbox').each(function (index, element) { 
    element.MaterialCheckbox.check();
})
krisanalfa
  • 6,268
  • 2
  • 29
  • 38
2

I found the best way to do this without getting into the MDL JS API was to just trigger a click on the <input type='checkbox'> if it was checked:

if ($('#mycheckbox')[0].checked) {                                      
    $('#mycheckbox').trigger('click');
}

For my use case, I had two checkboxes which should never both be set, but can have neither set, and used the following:

$(function() {
    $(document).on('change', '#mycheckbox1', function() {
        if(this.checked && $('#mycheckbox2')[0].checked) {
            $('#mycheckbox2').trigger('click');
        }
    });
    $(document).on('change', '#mycheckbox2', function() {
        if(this.checked && $('#mycheckbox1')[0].checked) {
            $('#mycheckbox1').trigger('click');
        }
    });
});
Chris Wheeler
  • 1,623
  • 1
  • 11
  • 18
1

Sorry, but none of the solutions provided above worked for me as of v1.2.1

This is what I did, and it´s working ok for me.

$(".mdl-checkbox").on("change", function() {
  if ($(this).hasClass("is-checked")) {
    return $(this).children().first().attr("checked", true);
  } else {
    return $(this).children().first().removeAttr("checked");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hope it helps.

biojazzard
  • 563
  • 4
  • 8
0

UPDATE: the following answer is bad practice (according to one of the project maintainers) and a solution is provided in patched version 1.0.1. I will still leave the answer here for future reference.


the following solution is recommended only if you are upgrading your DOM elements to MDL components manually (i.e. var myCheckBox = new MaterialCheckbox(el);).


The easiest way that I came up with was to get access to the actual MDL component (instead of DOM elemnt)**. So if your HTML looks like the following:

<label class="my-checkbox" for="checkbox-1">
  <input type="checkbox" id="checkbox-1" class="mdl-checkbox__input" checked />
  <span class="mdl-checkbox__label">Checkbox</span>
</label>

Then you would have something like this:

var domEl = document.getElementById("my-checkbox")
var mdlComp = new MaterialCheckbox(domEl)

// now you can just use the functions provided by the MDL component
mdlComp.uncheck();

Other methods provided by MaterialCheckbox are check, disable, and enable


** I have written an article on how MDL works here

Community
  • 1
  • 1
Yan Foto
  • 10,850
  • 6
  • 57
  • 88
  • To be clear, this is *bad practice*. It may provide you with access to object methods the component developers did not intend for you to have access to. You should *ALWAYS* use the component handler. If you need access to something and you don't, then file a bug with the component and use-case. We may have accidentally not exposed an internal component as a widget or may have simply not thought of a given use-case where widgetizing a component was needed (which can be changed.) – Garbee Jul 15 '15 at 01:06
  • @Garbee I sure did file a bug: https://github.com/google/material-design-lite/issues/1034 – Yan Foto Jul 15 '15 at 06:15
0

Just change this:

$("#checkbox-1").prop('checked',true);

into this:

$("#checkbox-1").prop('checked',true).change();

That worked for me (using material design 1.0.4)

Wolf
  • 9,679
  • 7
  • 62
  • 108
MrPotato
  • 1
  • 1
0

Found a nice little solution that simply re-sets the class 'is-checked' on the parent <label> tag.

var te = $('#some-checkbox').prop("checked", false).parent();
te.removeClass('is-checked');

.addClass() to set and refactor the code to suite of course.

Mike
  • 391
  • 4
  • 11
  • this "hard" manipulation of classNames is definitely not a good practice, you should use component API - .check() as mentioned abowe – Marek Jalovec Jun 27 '16 at 08:49