material-design-lite: Dynamically changing input (.mdl-textfield) text with Javascript does not mark it as dirty

Demo: http://codepen.io/anon/pen/qdYEWw

Expected behavior: Clicking the button (and thus modifying the text) should add the is-dirty class to the input’s parent, which would then move the label to the top of the input.

Actual behavior: Upon clicking the button, the input’s value changes, but the input’s parent is not marked as dirty, so the label appears on top of the input’s text.

Side note: I’ve had a lot of fun playing with this so far. Keep up the good work!

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (2 by maintainers)

Commits related to this issue

Most upvoted comments

The is-dirty class is applied on the div that contains the textfield. So you need to apply this class on this div. Try it: http://codepen.io/anon/pen/EjRLVL

I came across this again. Something like below should work for everyone:

  //MDL Text Input Cleanup
  function mdlCleanUp(){
    var mdlInputs = doc.querySelectorAll('.mdl-js-textfield');
    for (var i = 0, l = mdlInputs.length; i < l; i++) {
      mdlInputs[i].MaterialTextfield.checkDirty();
    }  
  }

We don’t support react, but the PR was merged and the methods should be available in the current stable. To check dirty state after changing the value, call the MaterialTextfield.checkDirty() method against the node where the js class is assigned. For example: document.querySelector('mdl-js-textfield).MaterialTextfield.checkDirty().

This still needs proper documenting before closing this issue out.

Add a unique class to all inputs that you need to check for dirty, select them all with querySelectorAll, then apply the MaterialTextfield.checkDirty() method to each via a loop.

var dialogInputs = doc.querySelectorAll('.dialog-inputs');
for (var i = 0, l = dialogInputs.length; i < l; i++) {
  dialogInputs[i].MaterialTextfield.checkDirty();
}

You have to manually add the ‘is-dirty’ class to the parent, like this: $(‘#sample3’).parent().addClass(‘is-dirty’);

Example: http://codepen.io/anon/pen/mVyzBr