0

I know this question has been answered many times. However, my code does not seem to work despite I literally got it from the internet. I probably do something wrong, but what exactly I could not figure out.

I put the following JS code inside the HTML <head> tag :

$(document).ready(function() {
  var imwidth = $('#jspp').width(); 
  var imheight = $('#jspp').height(); 
  if (imwidth < imheight) {
   $('#jspp').width(100);
  } else {
   $('#jspp').height(100);
  }	 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div>
    <img id="jspp" src="https://placehold.it/300x300">
  </div>
</body>

Does somebody have an idea of what could be wrong?

Thanks in advance!

7
  • 2
    You're missing a ')' at the end of your jQuery. Check your console.log for errors. Commented Oct 16, 2017 at 16:49
  • Don't assume everything you copy straight from the internet is correct and valid :) Commented Oct 16, 2017 at 16:53
  • @MCMXCII thanks! I corrected it, but it still does not work Commented Oct 16, 2017 at 16:54
  • @FluffyKitten hahah, unless you're a beginner like me I guess Commented Oct 16, 2017 at 16:54
  • If it's still not working, make sure you are including jQuery. Have you got any errors in your console? Commented Oct 16, 2017 at 16:55

1 Answer 1

1

Here you go with a solution https://jsfiddle.net/027q85w8/

$(document).ready(function() {
  var imwidth = $('#jspp').width(); 
  var imheight = $('#jspp').height(); 

  if (imwidth < imheight) {
    $('#jspp').css('width', '100px');
  } else {
    $('#jspp').css('height', '100px');
  }   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <img id="jspp" src="http://via.placeholder.com/350x150">
</div>

Two things were wrong in your code.

  • Missing ) at the end of document.ready
  • Assigning of 100px in width & height.

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.