5

I am a learning javascript guy and I am trying to add the Image Hover Shadow Effect With CSS to my images and for that to work , I have to add an class to all my img tags , which is not possible manually so i am trying to write a javascript that does that and uses jquery , to do so .

it needs to add an class to img tag , such as this

From this

<img border="0"  height="75" src="http://3.bp.blogspot.com/-qhxaAUAJUVQ/TeocW4AuIiI/AAAAAAAABCo/IYy7hQ6-5VQ/s200/CSSEditLogo1.jpg" width="75"/> 

to

<img border="0" class="imagedropshadow" height="75" src="http://3.bp.blogspot.com/-qhxaAUAJUVQ/TeocW4AuIiI/AAAAAAAABCo/IYy7hQ6-5VQ/s200/CSSEditLogo1.jpg" width="75"/>

Can anyone help :)

Thanks

5 Answers 5

19

To add the class to all of the images on your page with jQuery:

$("img").addClass("imagedropshadow")

Without jQuery is is not very difficult either:

var images = document.getElementsByTagName("img");
var i;

for(i = 0; i < images.length; i++) {
    images[i].className += " imagedropshadow";
}
Sign up to request clarification or add additional context in comments.

1 Comment

The activity of adding the class can be made simpler as follows images[i].classList.add('imagedropshadow');
2

Are you sure you need the class. If you are planning to add a css class to all images and hook the shadow to it, you should be able to do it on the img tags directly.

img:hover{
 /*properties*/
}

1 Comment

THANKS THIS WORKS .. $("img").addClass("imagedropshadow"); this did not work though :( anyway THANKS !
2

give an ID to your image, let's say it's imgId and then write this jquery (if you want to add class to any specific img tag.)

$(document).ready(function(){ 
      $('#imgId').addClass('imagedropshadow');
});

for all img tag simply use this;

$("img").addClass("imagedropshadow"); 

Comments

1

You can use the addClass function with jQuery. See the API here for examples.

3 Comments

<script> $("img").addClass("imagedropshadow"); </script> so is this how i am supposed to do so ? it is not working
place your code inside $(document).ready(function(){ $("img").addClass("imagedropshadow"); });
Thanks img:hover{ /*properties*/ } worked jsfiddle is a gr8 site for debugging THANKS :) @Vivek @James Allardice
1

You can try this:

$('img').addClass('imagedropshadow');

But I suggest you that you add an id or general class into the img tag for selecting.

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.