David
David

Reputation: 4007

Set div width from a function

i need to set the width of a div container to be 100% - 150px

How can this be achived using jquery?

Upvotes: 0

Views: 266

Answers (4)

Last Rose Studios
Last Rose Studios

Reputation: 2481

div = $('div');
parent_width = div.parent().width();
div.width(parent_width - 150);

Upvotes: 0

Kamil Lach
Kamil Lach

Reputation: 4629

Try this:

  $("#id").width('100%');
  var width = $("#id").width();
  $("#id").width(width - 150);

JQuery documentation: http://api.jquery.com/width/

Upvotes: 1

Thunraz
Thunraz

Reputation: 570

This should work:

$('#yourdiv').width( $('#yourdiv').width() - 150 )

Also have a look at the jQuery Documentation.

Upvotes: 2

nheinrich
nheinrich

Reputation: 1841

If you're basing it on the window you can use:

var width = $(window).width() - 150;
$("#container").css("width", width);

Upvotes: 0

Related Questions