Dhamu
Dhamu

Reputation: 1752

Add class to images in JQuery click function

In click function all images,

<script type="text/javascript">
  $(".blurall").click(function(){
  $("[img='.jpg']").addClass("opacity");
  });
</script>

My css

.opacity{
    opacity:0.5;
}

Not working any one help me!

Upvotes: 0

Views: 137

Answers (2)

Dhamu
Dhamu

Reputation: 1752

your selector is wrong, Use img[src$='.jpg'] like this

<script type="text/javascript">
  $(".blurall").click(function(){
      $("img[src$='.jpg']").addClass("opacity");
  });
</script>

try this...

Upvotes: 1

Sampson
Sampson

Reputation: 268424

Make sure your DOM is ready before this can be called. Additionally, you forgot the quotes around your first class selector. I swapped out the old $.click for the preferred $.on. Lastly, updated your selector so that the src must end with .jpg in order to be matched.

$(function(){
    $( ".blurall" ).on( "click", function() {
        $( "img[src$='.jpg']" ).addClass( "opacity" );
    });
});

Upvotes: 7

Related Questions