user14331657
user14331657

Reputation:

Why is this logical operator not working?

shrimpcook01 css-top value

im using the code

setInterval(function() {
  if (document.querySelector("#shrimpCook01").style.top > "400px") {
    alert("789")
  }
}, 200);

to check where shrimpcook01 is, but im receiving an alert message even though the div's top value is less than 400px.

Upvotes: 1

Views: 110

Answers (2)

Anarno
Anarno

Reputation: 1640

You can transform your string values easily.

setInterval(function() {
  const height = document.querySelector("#shrimpCook01").style.top.split('px')[0];
  if (Number(height) > 400) {
    alert("789")
  }
}, 200);

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43265

It would be doing lexicographical comparison of strings. Use parseInt to convert the value to number, and compare it against 400 number, not "400px" string.

Upvotes: 0

Related Questions