Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery attr is not working

I have image in container and I have set width and height attributes for this image in CSS. But I need to add loading image and it is smaller than actual pictures that will be shown in container, so I need to change size of image while it is loading. But when I set width and height attributes, image is shown in size that is set in CSS.

$(img).attr("src", "loading.gif");
$(img).show();
$(img).attr("width", "100px");
$(img).attr("height", "100px");
like image 895
newbie Avatar asked Jul 06 '26 13:07

newbie


1 Answers

You're confusing attributes with CSS:

$(img)
  .attr("src", "loading.gif")
  .css({width:100,height:100})
  .show();

Of course this assumes that img is actually representing an image:

var img = $("img.myImage");

If you don't have an image, you'll need to create one, and append it to the DOM:

$("<img>")
  .attr("src", "loading.gif")
  .css({width:100,height:100})
  .appendTo("body");
like image 154
Sampson Avatar answered Jul 08 '26 03:07

Sampson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!