0

How can I make an alert in jQuery when a price is higher than 400.

Look at there: <span class="element-price-value">424,97</span>

If value is higher than 400, I need to make an overlay modal popup that continue is not allowed and page need to refeshed.

Here is what I did but didn't work...

var price = $(".element-price-value");

if(price > 400){
    alert("Hello! I am an alert box!!");
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Chocoprins18
  • 115
  • 2
  • 11

4 Answers4

2
if(Number(price.text().replace(',', '.')) > 400){
  alert("Hello! I am an alert box!!");
}

You need to get the contents of <span>. Else you're just refering to the object. Then replace "," with "." and finally get the number from it.
You try to compare numbers, not text, nor objects.

You can see how price, price.text() and Number(price.text()); differ

var price = $(".element-price-value");

if(Number(price.text().replace(',', '.')) > 400){
  alert("Hello! I am an alert box!!");
} else {
  alert(price);
  alert(price.text());
  alert(Number(price.text()));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="element-price-value">424.97</span>
Peter
  • 1,844
  • 2
  • 31
  • 55
  • But this runs when the page loads not when value is higher. – Chocoprins18 Oct 03 '18 at 09:42
  • The first 3 do, but the one in the `if()` condition does not. I know edited it into an `else{}` block. It would only run if the first condition was false. Just so you can see the difference. – Peter Oct 03 '18 at 10:01
1

This code will also work if you will have in span 400,97

$(document).ready(function(){

  // Getting the value in span
  var value = $('.element-price-value').text();
  // or vanilla js that is faster
  // var value = document.querySelector('.element-price-value').innerText;
  
  // Converting to float and changeing comma to .
  value = parseFloat(value.replace(',','.'))
  
  if(value>400) alert('Higher than 400')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="element-price-value">400,97</span>
qiAlex
  • 4,290
  • 2
  • 19
  • 35
-1

Right now, you are getting only .element-price-value span, not the value inside. Please, use var price = $(".element-price-value").text(); to get the value inside that span, and parse it with parseInt() function.

So, the final result must be like: var price = parseInt($(".element-price-value").text());

oisar
  • 9
  • 1
  • This logic is flawed. As you `parseInt()` only the value before the `,` is used, hence `400,97` does not trigger the `alert()`, when it should. – Rory McCrossan Oct 03 '18 at 09:27
-2

If you have only a value inside span tag you can accomplish result like this:

var price = parseInt($(".element-price-value").text());
if(price > 400){
    alert("Hello! I am an alert box!!");
}
Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
narayansharma91
  • 2,273
  • 1
  • 12
  • 20
  • This logic is flawed. As you `parseInt()` only the value before the `,` is used, hence `400,97` does not trigger the `alert()`, when it should. – Rory McCrossan Oct 03 '18 at 09:26