Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set height , width to image using jquery

Tags:

jquery

Is there any way to set height and width of an image using jquery? The following is my code

var img = new Image();  
 // Create image
$(img).load(function(){                 
    imgdiv.append(this);
}).error(function () {  
    $('#adsloder').remove();
}).attr({ 
    id: val.ADV_ID,  
    src: val.ADV_SRC,
    title: val.ADV_TITLE,
    alt: val.ADV_ALT
});

Thanks.

like image 854
Vicky Avatar asked Feb 02 '10 12:02

Vicky


People also ask

How to set width of image using jQuery?

jQuery width() Method The width() method sets or returns the width of the selected elements. When this method is used to return width, it returns the width of the FIRST matched element.

How do you change the size of an image in HTML?

There is no command for changing an image size. Image dimensions are 'properties' which can be expressed in either the HTML <img> element, as width="150" height="100" attributes in the tag; or, the CSS as style rules applying to specific images.

How do you change the size of an image in CSS?

Sometimes, it is required to fit an image into a certain given dimension. We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container.


2 Answers

You can call the .height() and ,.width() setters:

var img = new Image();  
 // Create image
$(img).load(function(){                 
    imgdiv.append(this);
}).error(function () {  
    $('#adsloder').remove();
}).attr({ 
    id: val.ADV_ID,  
    src: val.ADV_SRC,
    title: val.ADV_TITLE,
    alt: val.ADV_ALT
}).height(100).width(100);
like image 184
Nick Craver Avatar answered Sep 22 '22 07:09

Nick Craver


$(".img1").css('height','10');
$(".img1").css('width','10');

OR

$(".img1").attr('height','10');
$(".img1").attr('width','10');
like image 27
sekhar.k Avatar answered Sep 18 '22 07:09

sekhar.k