jquery how to re-apply themes.js when a new element is added via js

Asked by jsherk

When a page loads, the /themes/cloud/js/theme.js file has a .ready() function which applies/removes classes to different elements.

I have added additional js that will allow a user to add and remove new elements to the page, but when I add the element, the class changes applied by themes.js are not applied, since they were run when the page was loaded.

How can I have the .ready() function run again when the user adds a new element to the page? Is there a way to call these functions from javascript to run them again so the newly added elements will get the class changes as well?

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
PHPDevShell Edit question
Assignee:
No assignee Edit question
Solved by:
jsherk
Solved:
Last query:
Last reply:
Revision history for this message
jsherk (jeff-forerunnertv) said :
#1

It appears that once the .ready() function is called there is no way to call it again without re-loading the page, which won't work because the users manually added elements would be removed.

But I found this solution... We can move all the code for making the class changes into a separate function, and then use the .ready() to call that function. Now if we need to reapply those class changes from js at a future time, we can manually call the function.

In the example below I have moved all the code into a function called jq_theme_js_ready() so whenever I want to apply the changes that theme.js made when the document loaded, I just call jq_theme_js_ready() .

In the theme.js file, change this first line:
$(document).ready(function() {

To these lines:
$(document).ready(jq_theme_js_ready); //Notice there are NO parentheses() after the jq_theme_js_ready
function jq_theme_js_ready() {

And in the theme.js file change the very last line from this:
});

To this (remove the parenthese):
};

Revision history for this message
jsherk (jeff-forerunnertv) said :
#2

Can I request that this be moved on the WISHLIST as a permanent type of change?

Also, I am sure you can come up with a better function name than jq_theme_js_ready but this will work for me for now!

Thanks

Revision history for this message
Greg (gregfr) said :
#3

I agree, this is a drawback of jQueryUI. It could be done more silent but that would probably induce performance it.
I'm adding a wishlist report.