shampouya
shampouya

Reputation: 416

Why can't my if statement in jquery detect image size?

The If - Else statement in my code below seems to be causing an error. When the image is clicked, I'm trying to have a google window open if the image is 100px by 100px in size, and a yahoo window open in any other circumstance. Any idea what's making the if statement error out?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">


$(document).ready(function() {
$("#testImgID").click(function(){

   var imgWidth = $(this).width();
   var imgHeight = $(this).height();

   if( imgWidth == 100 && imgHeight == 100)
   {
        window.open("http://www.google.com");
   else
   {
        window.open("http://www.yahoo.com");
   }
});
});

<p>
<img width="100" height="100" id="testImgID" alt="Setup client email capture.jpg" src="/wg/PayrollSolutions/PublishingImages/RCP_setups_theatrical_assignedTo_table.png" style="margin: 5px"/>
</p>

Upvotes: 0

Views: 39

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40434

You have a syntax error, you didn't close the first if

$(document).ready(function() {
    $("#testImgID").click(function() {

        var imgWidth = $(this).width();
        var imgHeight = $(this).height();

        if (imgWidth == 100 && imgHeight == 100) {
            window.open("http://www.google.com");
        } else { // } was missing
            window.open("http://www.yahoo.com");
        }
    });
});

Always check the developer's console (f12 usually) in order to check for syntax errors, you should see: Uncaught SyntaxError: Unexpected token else and in which line the error is.

Upvotes: 1

Related Questions