Saltar navegación

[Resuelto] Some JS don’t work on view

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
The user was using a custom Javascript code to format a number to be displayed with a "space" after each 3 digits

Solution:
The code was not correct, it only handled thousands and was not correct for bigger numbers.

This support ticket is created hace 4 años, 8 meses. 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.

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

Supporter timezone: Africa/Casablanca (GMT+01:00)

Etiquetado: 

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por romanB-3 hace 4 años, 8 meses.

Asistido por: Jamal.

Autor
Mensajes
#1740167

Hi,
On on this page enlace oculto I have this code

function formatNumber(num) {
  return num.toString().replace(/(d)(?=(d{3})+(?!d))/g, '$1 ');
}
function changePriceFormat(){
  //Code en page <em><u>enlace oculto</u></em>
  //prix en CFA
  var prix_cfa = jQuery(".prix-cfa");
  for (var i = 0; i < prix_cfa.length; i++) { 
      var price = parseInt(prix_cfa.eq(i).text());
      prix_cfa.eq(i).text(formatNumber(price));
   }
}

jQuery(document).ready(function( $ ){
  	changePriceFormat();
});

jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
	changePriceFormat();
});

but on front (enlace oculto) the code doesn't apply, and my figures still are unformated.

On the other hand, the same code works properly here enlace oculto

Thank you.

#1740367

Hello and thank you for contacting the Toolset support.

As you may know from our support policy, custom code is out of the scope of this forum. We'll, still, help you if this code was suggested by us. In that case, please let me know which ticket this code was suggested in it.
https://toolset.com/toolset-support-policy/

Otherwise, I'll suggest, to asking in a general-purpose forum such as StackOverflow, or hire a developer to help you with it. Check our partner's list here https://toolset.com/contractors/

As a quick hint, I think that the code adds a space after the thousands, and that this code is not very generic it will only work for thousands, it won't work for higher numbers, you may want to change the second line with:

return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
#1740405

My issue is resolved now. Thank you!