Skip Navigation

[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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Shane 4 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1549825

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.

I recently applied an update to Toolset so don't suppose there are any known issues? The code is as it always was and referenced this page for it - https://stackoverflow.com/questions/2632359/can-jquery-add-commas-while-user-typing-numbers. My code is and was exactly as in the code snippet (only swapping $ for jQuery) but just doesn't run any more.

Any ideas why this might be?

#1550171

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

Thank you for getting in touch.

On the page where the jQuery is being executed could you send me a screenshot of any error that is being generated in the website's console ?

It could be that an error is being thrown which is causing the javascript to break.

Thanks,
Shane

#1552051

Hi Shane

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.

#1552211

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

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.

Thanks,
Shane

#1552263

hi Shane - of course.

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.

#1552437

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

I would need the access details from you. I've enabled the private fields for your next response so that you can send them to me.

Thanks,
Shane

#1552651

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

I took a look at this for you and your code is firing when the field is being typed in.

However this here seems to be a custom shortcode.

 [urlparam htmltag="input" type="text" name="wpv-wpcf-finance-min-loan-formatted" id="wpv_control_textfield_wpv-wpcf-finance-min-loan-formatted" attr="value" param="wpv-wpcf-finance-min-loan-formatted" class="formatnum" /]

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.

Thanks,
Shane

#1553317

Hey Shane

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.

#1553333

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.

#1553687

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

Thank you for the update.

What you can do is mark this ticket as resolved or closed.

Thanks,
Shane