Materialize: problem ith checkboxes & radios when <label> is before the <input>

Created on 14 Jan 2015  路  5Comments  路  Source: Dogfalo/materialize

the elements only work if out HTML looks like this:

<form>
  <input>
  <label></label>
</form>

However usually we see CMSs and apps using a structure like this:

<form>
  <label></label>
  <input>
</form>

or even this:

<form>
  <label>
    <input>
  </label>
</form>

Currently these elements only work in Materialize if the <label> element is after the <input>

Most helpful comment

This slightly more robust version of @erichschroeter's workaround did the trick for me:

$.each($(':checkbox'), function(k, v) { var label = $('label[for="' + this.id + '"]'); $(this).insertBefore(label); });

All 5 comments

This is just a limitation of css's sibling selector. We don't have any changes planned at this moment

Is there any known workaround?

Well, for those that come across this still wondering if there's a workaround, I think I found one. Just include jquery and include the following snippet.

<script>
/* Fix bug where materializecss checkboxes only render if before labels. */
$.each($(':checkbox'), function(k, v) {
    var label = $(this).closest('label');
    $(this).insertBefore(label);
});
</script>

This slightly more robust version of @erichschroeter's workaround did the trick for me:

$.each($(':checkbox'), function(k, v) { var label = $('label[for="' + this.id + '"]'); $(this).insertBefore(label); });

Thank you @erichschroeter (and @gvankeerberghen ), this piece of code is the only thing that saved me (worked for 'Select' as well).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

djensen47 picture djensen47  路  3Comments

ruslandzhumaev picture ruslandzhumaev  路  3Comments

locomain picture locomain  路  3Comments

serkandurusoy picture serkandurusoy  路  3Comments

ericlormul picture ericlormul  路  3Comments