2
I have a page that has a form
In this form, it contains some inputs, which have classes, e.g.:
<input type="text" class="datepicker">
<input type="text" class="marcara-cpf">
And the configuration of this of some plugins is done in a js file
$(".datepicker").config...
$(".mascara-cpf").mask("000.000.000-00")
It turns out that when the page is loaded by ajax, it does not arrow the configuration of those plugins
, having to make the call again from that setting. Which makes the code repetitive, every place that has a Cpf mask, have to do this... where I could just go to class
mascara-Cpf for the desired input.
I read in some places, that the implementation of AMD
could solve (using require.js)
But I don’t know how hard it will be to implement this require.js across the app
There is another elegant way to solve this problem with pages loading in ajax?
The problem with this method is that Mutation Events as
DOMNodeInserted
are obsolete, and may not exist in future browsers. https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Mutation_events– bfavaretto
Then in this case it will need to call the 'loadContent' method manually, or look for another event that monitors when a DOM element is added.
– Tobias Mesquita
Manually calling seems a good one in his case, since he knows exactly when new elements are loaded.
– bfavaretto
Summarizing, create functions to assemble these elements and call them within the page that is loaded by ajax always, this?
– Rod
Yes, but remember to delimit the scope, for example, if you are adding only one row to a table, only type the elements of this row.
– Tobias Mesquita
To add a row in the table dynamically, ai only for that line that was add né, because the contrary would set the plug for all, can lose values, this?
– Rod
Normally I create loadmask functions and in the window load I call them by ex, have problem if I pass $(.phone). Mask, even though there is no element with the phone class? That there in the case of masks I use a function and call for everything
– Rod
@Rod, when you make $(".mask"). Mask(), it assembles a list of all elements with the mask class on the page, so it will resort to all elements of this list by calling the Mask method for them. If the list is empty then the method will not be called.
– Tobias Mesquita
However, to prevent the Mask() method from being called repetitively for the same element, you must delimit a scope for your selection, say you receive an html by AJAX, then you can do something like this: var scope = $(html); $(".mascara", scope). Mask(); this way only the elements with the mask class that came through the AJAX request will be affected.
– Tobias Mesquita
@Rod, I’ve made some changes to the answer.
– Tobias Mesquita