[Resolved] JQuery code which previously worked has suddenly stopped
This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
I previously added some JQuery to a page on our site which is designed to format a number as it's being entered into an input box with the class "formatnum" attached to it so that thousand separators are added into it and this was working fine but has suddenly stopped working for some reason and I can't figure out why.
There's not, no, and that's the odd thing. (There actually was one but that was related to JetPack so once that was disabled that error went away and there were none showing after that however the code still didn't seem to run as it did before).
I tested the code via Codepen (so outside of where I'm using it within Toolset) and it ran fine, I also modified it to include an alert just to check the code was actually being triggered and the alert showed up fine, it's just the script which adds in thousand separators to numerical values has stopped working for some reason.
Would you mind sending me a link to the page as well as the code that you are using. Meaning the exact code that you've pasted into your JS editor with the correct classes added.
I want to see if I can run the code manually from the frontend.
The page is hidden link but is behind a login, do you need site access details or can you get them from my account? What's the best way to provide them if you need them? Here is the code from the JS portion of the view (it includes other scripting which performs other tasks which I'm including for the sake of completion)
// function to format numbers with thousands separator
jQuery('input.formatnum').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;
//alert("Testing");
// format number
jQuery(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(d{3})+(?!\d))/g, ",")
;
});
});
// set the max loan val equal to the min loan val
jQuery("input#wpv_control_textfield_wpv-wpcf-finance-min-loan-formatted").keyup(function() {
var newVal = jQuery("input#wpv_control_textfield_wpv-wpcf-finance-min-loan-formatted").val();
newVal = newVal.replace(/,/g, "");
jQuery("input#wpv_control_textfield_wpv-wpcf-finance-max-loan").val(newVal);
jQuery("input#wpv_control_textfield_wpv-wpcf-finance-min-loan").val(newVal);
});
jQuery("#wpv_control_textfield_wpv-wpcf-finance-min-loan-formatted").keyup(calc);
jQuery("#totalcost").keyup(calc);
function calc() {
if (jQuery("#wpv_control_textfield_wpv-wpcf-finance-min-loan-formatted").val().length !==0 && jQuery("#totalcost").val().length !==0) {
// calculate ltv
var maxltv = ((Number(jQuery('#wpv_control_textfield_wpv-wpcf-finance-min-loan').val().replace(/[^0-9.-]+/g,"")) / Number(jQuery('#totalcost').val().replace(/[^0-9.-]+/g,""))) * 100).toFixed(1);
// now display it and update the max ltv field
jQuery('#wpv_control_textfield_wpv-wpcf-finance-max-ltcltv').val(maxltv);
jQuery('#calcltv').html(
"LTV/LTC calculated at " + maxltv + "%"
);
}
else {
jQuery('#calcltv').html(
""
);
}
}
jQuery(document).ready(function() {
calc();
});
So it'll look for elements with a class of "formatnum" then run the script to insert commas where required as the user types (to make the number appear like a properly formatted number with thousand separators). Let me know if you need anything else.
It is actually out of our support scope to debug custom coding but from what I can see the problem is getting the code to apply the changes to the field because the field does trigger the code.
Not sure what is going on but I've just checked and code is actually working as before now which is just bizarre since I'd made no changes to the site/code yet (other than to swap the $ for jQuery in the code which I think you'd adjusted as it was throwing errors by using the $, however that was just restoring it to the way it was before you helped me).
The only other thing I can think of is that I cleared the Siteground cache to check something else, so even though the cache itself is actually disabled, perhaps something was being cached after all? I'll deactivate the SG Optimiser plugin for now to be safe and see if that helps.
Ok this is even weirder now, it's stopped working again before I made any more changes!
*EDIT* Annnd it's working again. Something funky is going on 🙂 Feel free to ignore this for now and I'll check the site again later, if all is okay then I'll presume it was more of a caching/hosting issue. I'll keep my Codepen to check on it again in the future however.