Here is a code:
<span id="dpos">30</span>
<script>
var pxwidth;
$( "#id" )
.bind( "keydown", function( event ) {
pxwidth = $("#dpos").text();
if (pxwidth === 0)
{
// do something
}
});
</script>
Here I am trying to get the value of the span element that is used in a conditional (if/else statement). But the statement does not execute. What is the correct usage of if (pxwidth === ?)?
Don’t use
===to compare a string with a number.Instead of:
Use:
===only returnstrueis both values are equal in value and type.$("#dpos").text()returns aString,0is aNumber.Resulting in you comparing them like this:
0 === "0", which is false.More info on comparison operators here.