Evanss
Evanss

Reputation: 23173

jQuery - add a set width to div?

I need to add a set number of pixels to a div with jQuery.

Is there an easy command for this or will I need to write some code that first finds the width of the element and then adds the set amount?

Upvotes: 2

Views: 11242

Answers (2)

Jeff
Jeff

Reputation: 12418

Or a third alternative,

$("#mydiv").css("width", "+=10");

Upvotes: 8

Eric
Eric

Reputation: 97681

You'll need to write the code yourself. However, doing what you want is trivial:

$('#mydiv').width($('#mydiv').width() + 10);

Or another way, which will work for a whole set of elements:

$('.mydiv').width(function(index, value) {
    return value + 10;
});

Upvotes: 5

Related Questions