Search code examples
jquerylandscapejsfiddleportrait

jquery is not working in VS but its working in jsfiddle.net


here is the jquery

$('#pl').find('img').each(function(i, elem) {
    var $this = $(this),
    ratio = $this.width() / $this.height();

    $this.addClass((ratio < 1) ? 'portrait' : 'landscape');
});

html:

<div id="pl">
<img src="#" width="60" height="30" />
<img src="#" width="30" height="60" />
<img src="#" width="40" height="80" />
<img src="#" width="90" height="50" />
<img src="#" width="60" height="30" />
<img src="#" width="40" height="80" />
</div>

and here's the example in jsfiddle.net >>> SAMPLE


Solution

  • Include Jquery in Head

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
     <script>
     $(window).load(function () {
         $('#pl').find('img').each(function (i, elem) {
             var $this = $(this),
                 ratio = $this.width() / $this.height();
    
             $this.addClass((ratio < 1) ? 'portrait' : 'landscape');
         });
     });</script>