You are viewing limited content. For full access, please sign in.

Question

Question

Thousands delimiter breaks my code

asked on February 8, 2018 • Show version history

Having thousands delimiters set to on in either field breaks this code, it doesn't populate #q225.  Am I missing something, is there a syntax error?

/*Thousands delimiters in either field breaks this code, it doesn't populate #q225 */
$(document).ready(function(){
  $(document).on('change', '.minVolt input', function(){
   if ($('#q223 input').val() < 5000){
		$('#q225 input').val(1000);
		}
	else if (($('#q223 input').val() >= 5000) && ($('#q223 input').val() < 25000)){
		$('#q225 input').val(2500);
		}
    else if ($('#q223 input').val() >= 25000){
	$('#q225 input').val("Volt Rating Exceeded");
		}
  });
});

 

0 0

Answer

SELECTED ANSWER
replied on February 13, 2018

To summerize, as Rui said, the answer is to use parseInt or parseFloat to remove the thousands delimiter:

 

parseInt($('#q223 input').val().replace(/,/g,'')) < 5000

 

((parseFloat($('#q255 input').val().replace(/,/g,'')) > (parseFloat($('#q252 input').val().replace(/,/g,'')) )))

0 0

Replies

replied on February 8, 2018

Hi Alon,

$('#q223 input').val() returns a string, not a number. So when thousand delimiter is enabled, the returned string is like "5,000". For Javascript, "5000" == 5000 is true but "5,000" == 5000 is false.

You can remove the thousand delimiter before doing comparison, like this:

parseInt($('#q223 input').val().replace(/,/g,'')) < 5000

0 0
SELECTED ANSWER
replied on February 13, 2018

To summerize, as Rui said, the answer is to use parseInt or parseFloat to remove the thousands delimiter:

 

parseInt($('#q223 input').val().replace(/,/g,'')) < 5000

 

((parseFloat($('#q255 input').val().replace(/,/g,'')) > (parseFloat($('#q252 input').val().replace(/,/g,'')) )))

0 0
replied on February 9, 2018

Thanks Rui!  That was very helpful!  Is there anywhere else I should have been looking for this answer?

0 0
replied on February 10, 2018

If you know it's Javascript issue, you can Google for the answer.

0 0
replied on February 12, 2018

What if I want to compare two numbers with thousands delimiters and decimal places?  Should I use parseFloat then?  That is what I am trying to do with this code, but it is not working the way I expect it to

 

$(document).ready(function(){
  $(document).on('change', '.BrkerTimingRed input', function(){
   if ((parseFloat($('#q255 input').val().replace(/,/g,'')) > (parseFloat($('#q252 input').val().replace(/,/g,'')) ))){
		this.setAttribute('style', 'background-color: tomato !important')
        $('#q264 input').val('F').change();
		}
	else if ((parseFloat($('#q256 input').val().replace(/,/g,'')) > (parseFloat($('#q252 input').val().replace(/,/g,'')) ))){
		this.setAttribute('style', 'background-color: tomato !important')
        $('#q264 input').val('F').change();
		}
	else if ((parseFloat($('#q257 input').val().replace(/,/g,'')) > (parseFloat($('#q252 input').val().replace(/,/g,'')) ))){
		this.setAttribute('style', 'background-color: tomato !important')
        $('#q264 input').val('F').change();
		}			
    else {
      $(this).css('background-color', 'white')
      $('#q264 input').val('PASS').change();
    }
  });
});

 

0 0
replied on February 12, 2018

The expression ((parseFloat($('#q255 input').val().replace(/,/g,'')) > (parseFloat($('#q252 input').val().replace(/,/g,'')) ))) works for me for number with thousand delimiters and decimal places.

You said it's not working the way you expected, what is the current behavior?

0 0
replied on February 13, 2018

Thanks Rui.  It works for me as well.  My problem with the code snip I sent was just in how that if statement worked.  I was trying to get it to apply to a specific field, but can't seem to get the syntax right.  For example this.setAttribute, how could I get it to apply explicitly to q255?  Something like #q255.setAttribute.

0 0
replied on February 13, 2018

You can use $('#q255 input').attr(attributeName, value). The "attr()" is a jquery function for setting attribute see http://api.jquery.com/attr/

The "setAttribute" is not a jquery function, see https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute. 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.