How to get the value within this span?
<span id="Preço">199,00</span>
I need to compare it with another so that it triggers an action when it is greater than 199.00.
How to get the value within this span?
<span id="Preço">199,00</span>
I need to compare it with another so that it triggers an action when it is greater than 199.00.
With jQuery you can test this way:
if( parseInt( $('#Preco').text() ) > 199 )
With JS Pure, you can test like this:
if( parseInt( document.getElementById('preco').innerHTML ) > 199 )
But beware of the pennies. As mentioned by @bfavaretto, if you have 199,50
the result will not be expected because the decimal in JS is separated by .
. The solution would be to change ,
to .
and use parseFloat()
:
var preco = $('#Preco').text();
preco = parseFloat( preco.replace( '.', '' ).replace( ',', '.' ) ); // solução do @bfavaretto
if( preco > 199 ) { ...
Another thing: give preference to not accentuating elements IDs to avoid bugs and miscellaneous incompatibilities.
var preco = $('#Preco').text();
preco = parseFloat( preco.replace( '.', '' ).replace( ',', '.' ) );
if( preco > 199 ) {
$('#Res').text( 'Mais que 199' );
} else {
$('#Res').text( 'Menor ou igual a 199' );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script><spanid="Preco">199,50</span>
<span id="Res"></span>