user1487070
user1487070

Reputation: 1

What's wrong with this jquery if then function

Here's my function.

<script>
  if( $('#bla').text() > 1 ) {
    alert( "yup!" );
  }else{
    alert( "nope!" );
  }
</script>

I tested it out numerous times and it doesn't open the alerts. What's wrong?

Upvotes: 0

Views: 90

Answers (2)

Malk
Malk

Reputation: 11983

($('#bla').text()>1) is comparing a string to a number, and it will always return false.

I assume you expect #bla to be a number so you should tell javascript to parse it as such: parseInt($("#bla").text(),10) or parseFloat($("#bla").text())

ETA: Upon review of @elclanrs's fiddle it seems that javascript can handle the conversion just fine. There must be something else happening here. The syntax looks fine; the only thing left is that $("#bla") is returning an empty array...

Upvotes: 2

Naveen
Naveen

Reputation: 687

You need to specify it inside a function.

<script>
$(document).ready(function () {
if($('#bla').text()>1){
   alert("yup!");
}else{
   alert("nope!");
}
});
</script>

Also post your HTML code since we need to know what exactly you are trying to parse and find out if its greater than 1. Mostly, jquery code executes before the html is parsed hence that could also be an issue why its not giving you the alerts.

Upvotes: -1

Related Questions