Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to initiate dynamically added elements such as floating label inputs #134

Open
omicr0n opened this issue Jul 17, 2018 · 5 comments
Open

Comments

@omicr0n
Copy link

omicr0n commented Jul 17, 2018

So I was just getting ready to ask how you do this then all of a sudden it dawned on me what to try and it worked so figured I'd still post in here in case someone else is looking for the solution to this problem since it doesn't appear to be in here anywhere and I couldn't find anything about it anywhere in the docs less I missed that memo.

But when injecting elements specifically to what this script styles such as the floating label technique and methods used to give inputs there "material" design if you inject your elements after $('body').bootstrapMaterialDesign(); then the input label won't float when you click it to make active and start typing and will just overlay on top of what your typing.

I tried calling $('body').bootstrapMaterialDesign(); again after injecting my elements on the page but that didn't change anything. After digging around and getting ready to ask how you handle this case that's when I noticed the 'body' selector this method is being called on so tried calling .bootstrapMaterialDesign(); on my new element(s) specifically and BAM they started functioning properly as floating label inputs.

Example

var newElementContainer = $('<div class="form-group bmd-form-group"></div>');
var newElementLabel = $('<label for="myInjectedInput" class="bmd-label-floating">Some Floating Label</label>');
var newElementItem = $('<input type="text" class="form-control" id="myInjectedInput">');

newElementContainer.append(newElementLabel);
newElementContainer.append(newElementItem);

// newElementItem.bootstrapMaterialDesign();  <-- calling this here or anywhere before the element 
// and/or the container it's in has been appended to the pages body wont work

$('#someForm').append(newElementContainer);

newElementItem.bootstrapMaterialDesign();

// or initiate it by the elements ID $('#myInjectedInput').bootstrapMaterialDesign();
// or add a class to all your injected inputs and initiate them all at once when your done using 
// the class selector $('.myInjectedInputs').bootstrapMaterialDesign();

So yeah... I don't know if this is all documented somewhere and/or the use of .bootstrapMaterialDesign(); or if that info is reserved for the pro version but there you go.

@groovemen
Copy link
Contributor

Helllo @omicr0n,

Thank you for your interest in working with our products and also we appreciate your help in solving this issue.

All the best,
Stefan

@annhorne
Copy link

This is amazing! Thank you @omicr0n for this solution. I'm busy building an application where I need to create some form fields dynamically; noticed that the floating label was no longer working on the added fields. Your solution fixed the problem for me! Yay!

@frumentius
Copy link

Hi @omicr0n @groovemen, Greetings. I've tried this solution on dynamically added elements of toggle button and checkbox and unfortunately it did not work. Have You tested it on toggle button and checkbox? Any suggestion to make it work?

@frumentius
Copy link

I have found the solution, for me I should be using $.material.init(); instead of .bootstrapMaterialDesign();

@shubham-chhimpa
Copy link

So I was just getting ready to ask how you do this then all of a sudden it dawned on me what to try and it worked so figured I'd still post in here in case someone else is looking for the solution to this problem since it doesn't appear to be in here anywhere and I couldn't find anything about it anywhere in the docs less I missed that memo.

But when injecting elements specifically to what this script styles such as the floating label technique and methods used to give inputs there "material" design if you inject your elements after $('body').bootstrapMaterialDesign(); then the input label won't float when you click it to make active and start typing and will just overlay on top of what your typing.

I tried calling $('body').bootstrapMaterialDesign(); again after injecting my elements on the page but that didn't change anything. After digging around and getting ready to ask how you handle this case that's when I noticed the 'body' selector this method is being called on so tried calling .bootstrapMaterialDesign(); on my new element(s) specifically and BAM they started functioning properly as floating label inputs.

Example

var newElementContainer = $('<div class="form-group bmd-form-group"></div>');
var newElementLabel = $('<label for="myInjectedInput" class="bmd-label-floating">Some Floating Label</label>');
var newElementItem = $('<input type="text" class="form-control" id="myInjectedInput">');

newElementContainer.append(newElementLabel);
newElementContainer.append(newElementItem);

// newElementItem.bootstrapMaterialDesign();  <-- calling this here or anywhere before the element 
// and/or the container it's in has been appended to the pages body wont work

$('#someForm').append(newElementContainer);

newElementItem.bootstrapMaterialDesign();

// or initiate it by the elements ID $('#myInjectedInput').bootstrapMaterialDesign();
// or add a class to all your injected inputs and initiate them all at once when your done using 
// the class selector $('.myInjectedInputs').bootstrapMaterialDesign();

So yeah... I don't know if this is all documented somewhere and/or the use of .bootstrapMaterialDesign(); or if that info is reserved for the pro version but there you go.

@omicr0n it will not works in all situation.

instead of calling .bootstrapMaterialDesign(); on that particular element you can call it on all element of that type

$("input").bootstrapMaterialDesign();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants