Make materialize labels move out of input box when input box is filled via javascript - css

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?

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.

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.

If you use labels, you will use:
$("label[for='idTag']").addClass('active');

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

Add class="active"into the label tag associated with the input field.

With JQuery you can use
$('#yourInputText').change();

If you are using older version (0.x) of Materializecss, use:
Materialize.updateTextFields();
instead of:
M.updateTextFields();

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 the componentDidMount() 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)

Related

Full Calendar/Angular - Handle date cell content when rendering

I'm using the full-calendar component for Angular and I want to customize the content of each date cell according to different conditions. For example if the date is invalid (I have a function that validates the date), I want to change the date's cell background color. Otherwise, if its valid I want to add a plus button inside to add events. Besides that I want to add custom animations and hover effects.
How can I take control of the date is being render ?
I manage to change the background color with dayCellDidMount hook:
dayCellDidMount: (arg) => {
if(!this.dateIsValid(arg.date, arg.isPast)){
arg.el.style.backgroundColor = "#eeeeee";
}
},
I don't think that's the best way to do it and I still can't manage to add the plus button inside the cell.
Can someone achieve this or something similar? Your help would be appreciated, thanks.
If you know another calendar to integrate with Angular that has this features I'm open to suggestions.

CSS for required fields in Google App Maker

I am currently working on setting up an input form in Google App Maker.
Some of the textboxes require an input and therefore the labels are tagged with an asterisk (*) when drag them to the page, e.g. "Birthday *".
But when I drag the textboxes into a panel in order to rearrange them, the asterisk keeps disappearing. How can make the asterisk appear again?
Why it happens?
Once you drop a form on a page App Maker will add the following CSS classes to form's widgets:
app-FormBody for the inner form panel
required for all input widgets with input required
Also somewhere in App Maker's internals the following CSS rules are defined:
/* Show asterisk only for direct children of 'app-FormBody' panel
marked with 'required' class */
...
.app-FormBody > .app-TextBox.required > .app-TextBox-Label:after,
... {
content: " *";
}
So, when you drop panel inside form body and drag your input inside that inner panel App Maker CSS rules stop working (the widgets are not direct children of app-FormBody anymore).
How to fix it?
you can try to override default App Maker styles
/* Note: there is no '>' selector */
.app-FormBody .app-TextBox.required > .app-TextBox-Label:after {
content: " *";
}
I'm not sure what side effects it can potentially cause...
You can explicitly add asterisk in binding
#models.MyModel.fields.FieldName.displayName + ' *'
Please, also keep in mind that App Maker will not automatically add those hidden styles to the widgets you'll add after form is generated.
The solution below worked for me, although there might be other ways of doing it too.
Please note: If you will use this only in one page, place the css code at the Page style level, otherwise, place it at the Global style level.
CSS:
.customRequired::after {
content: " *";
}
Once you've written the CSS, go to the onAttach event of the TextBox widget and use the following JS:
var elem = widget.getElement();
elem.children[0].classList.add("customRequired");
Additional notes: I tried using the appmaker default css class which is required but it didn't work. Furthermore, I only tested this on the TextBox widget.
Hope it helps!

Capturing clicks in a column of buttons (with dojo)

I am trying to capture the click event of a row using a column of "use" buttons. This is similar to the "selectable" feature in the Kendo grid, but having a button makes it more obvious for the user in our case.
Because there are multiple buttons (and I don't know if you can dynamically assign button id values), I tied a CSS class to the button, and I will use that to determine what row I am on when the user clicks the USE button.
Here is a dojo of what I am trying to accomplish, but for some reason, the click event (alert statement) is never executed.
http://dojo.telerik.com/UkIW/2
Can anyone spot the problem?
As far as I can see you're using jQuery and no Dojo code so far. But there are some mistakes here, first of all, you should put the event handler for hte buttons in the ready() handler as well, so move it inside:
$(document).ready(function() {
// ...
});
And then second, if you're binding to dynamic elements with jQuery, the preferred way of doing so is by adding it to a parent element (for example the <body>) and then adding a second parameter as shown below:
$(document).ready(function() {
// ...
$("body").on("click", ".use", function() {
// ...
});
});
I created a milestone in your example: http://dojo.telerik.com/UkIW/5

How to render checked checkboxes using CSS alone?

This is may be very noobish and a bit embarrassing but I am struggling to figure out how to make checkboxes 'checked' using CSS?
The case is that if a parent has a class setup (for example) I'd like to have all the checkboxes having setup as parent to be checked. I'm guessing this is not doable in pure CSS, correct? I don't mind using JS but am just very curious if I could toggle the state of the checkboxes along with that of their parent (by toggling the class).
Here's a fiddle to play around with.
A checkbox being "checked" is not a style. It's a state. CSS cannot control states. You can fake something by using background images of check marks and lists and what not, but that's not really what you're talking about.
The only way to change the state of a checkbox is serverside in the HTML or with Javascript.
EDIT
Here's a fiddle of that pseduo code. The things is, it's rather pointless.
It means you need to adding a CSS class to an element on the server that you want to jQuery to "check". If you're doing that, you might as well add the actually element attribute while you're at it.
http://jsfiddle.net/HnEgT/
So, it makes me wonder if I'm just miss-understanding what you're talking about. I'm starting to think that there's a client side script changing states and you're looking to monitor for that?
EDIT 2
Upon some reflection of the comments and some quick digging, if you want a JavaScript solution to checking a checkbox if there's some other JavaScript plugin that might change the an attribute value (something that doesn't have an event trigger), the only solution would be to do a simple "timeout" loop that continuously checks a group of elements for a given class and updates them.
All you'd have to do then is set how often you want this timeout to fire. In a sense, it's a form of "long polling" but without actually going out to the server for data updates. It's all client side. Which, I suppose, is what "timeout" is called. =P
Here's a tutorial I found on the subject:
http://darcyclarke.me/development/detect-attribute-changes-with-jquery/
I'll see if I can whip up a jQuery sample.
UPDATE
Here's a jsfiddle of a timeout listener to check for CSS classes being added to a checkbox and setting their state to "checked".
http://jsfiddle.net/HnEgT/5/
I added a second function to randomly add a "checked" class to a checkbox ever couple of seconds.
I hope that helps!
Not possible in pure css.
However, you could have a jQuery event which is attached to all elements of a class, thereby triggering the check or uncheck based on class assignments.
Perhaps like this:
function toggleCheck(className){
$("."+className).each( function() {
$(this).toggleClass("checkedOn");
});
$(".checkedOn").each( function() {
$(this).checked = "checked";
});
}

JavaScript calendar in Drupal (Scheduler module)

hi i have installed scheduler module from drupal.org. and i have set all the settings regarding this. now i can set publish and unpublished date in text box(there is format of date below text box).
i want to use java script calendar so when user click on the text box ,the calendar should open.
how can i do this
Since you're using Drupal you should have JQuery preloaded. You don't necessarily need JQuery to do this but it makes it easier, you'll use JavaScript either way.
With JQuery, simply setup a click listener on the textbox and have it call the whatever function triggers the calendar display like so...
Note: I used a simple ID selector below "#textbox" replace it with whatever way you want to select your textbox input. (eg. if you've given the HTML textbox input element an ID of "textbox" you can use my example)
$(document).ready(function(event) {
$('#textbox').live('click', function() {
//call your function here
});
});

Resources