Normally, with Materialize, the labels for text input boxes show up inside the input boxes until a user enter selects the box and enters text in it. However, when a box's value is filled via javascript, the label does not move out of the way. It stays in the box and overlaps with the text entered. Is there a way to trigger the label transition with javascript as well, so this doesn't happen?
8 Answers
More specifically, if you are using Materialize in Rails with turbolinks enabled, you will find that Materialize form fields which are prefilled with non-empty values are not active on page load.
The following method worked for me:
$(function() {
M.updateTextFields();
});
It will add class 'active'
to both corresponding labels and prefix icons.

- 198
- 1
- 13

- 2,181
- 2
- 23
- 32
-
3Materialize no longer works, you can replace it with `M.updateTextFields();` – gegobyte Dec 02 '18 at 08:24
-
If you don't use jquery: document.addEventListener("DOMContentLoaded", function () { // @ts-ignore M.updateTextFields(); }); – Pascal Ganaye Oct 05 '20 at 19:34
The label transition behavior is triggered by adding the active
class to the label's element. Thus, if you make your javascript add that class to the label (e.g. $('label').addClass('active')
) in addition to filling in the field, the label will transition out of the field just as it would when selected by a user action.

- 4,294
- 3
- 28
- 39
If you use labels, you will use:
$("label[for='idTag']").addClass('active');

- 2,287
- 8
- 23
- 49

- 1,470
- 1
- 12
- 8
The document.ready
event only fire once when turbolinks is working, so instead you need to tap into the turbolinks load event.
It happens so fast that if you want to see the animation, wrap it in a tiny timeout.
With timeout/visible animation
document.addEventListener('turbolinks:load', () => {
setTimeout(() => {
M.updateTextFields();
}, 100);
});
Without timeout/visible animation
document.addEventListener('turbolinks:load', () => {
M.updateTextFields();
});
Rails 5 with Turbolinks, and Materialize CSS 1.0.0

- 666
- 9
- 17
Add class="active"
into the label
tag associated with the input field.

- 1,081
- 1
- 15
- 27
With JQuery you can use $('#yourInputText').change();

- 62
- 1
- 12
-
1you have to be more explicit about your answer for the sake of beginners. – elquimista Nov 21 '16 at 08:14
-
Only yout need is a single line. $('input').change() or Materialize.updateTextFields(). It's all. – ferchoman09 Dec 06 '16 at 14:51
If you are using older version (0.x) of Materializecss, use:
Materialize.updateTextFields();
instead of:
M.updateTextFields();

- 3,719
- 8
- 17
- 30
For anyone looking for solution in ReactJS:
- link the value property of the input to some value in your Component's state. (Eg: value={this.state.name})
- Add the line
document.M.updateTextFields()
to thecomponentDidMount()
lifeCycle method of your Component.
The above solution helps when there are certain values which need to be prefilled into a Form. (Eg: Update Details Form)

- 785
- 8
- 17