knitevision
knitevision

Reputation: 3143

How do I pass an expression into jQuery .css method

I got N of li. And an element inside of li that needs to take a width depending on how many li there will be.

Here's what I get

var breadcrumb = $( '#crumbs ul li a' );
var bradcrumbCounter = $( '#crumbs ul li' );

breadcrumb.css("width", )

breadcrumbCounter.length returns 2.

I was planning to use it like in SASS, for example:

var widthCalculator = (100 / breadcrumbCounter.length) * 1%;

and insert it after "width", so that the width is set depending on the amount of li elements.

breadcrumb.css("width", widthCalculator);

But it doesn't seem to work. Where am I wrong?

UPDATE

Solved:

breadcrumb.css( {width: 100 / breadcrumbCounter.length + '%'} );

Upvotes: 1

Views: 58

Answers (1)

Sten Muchow
Sten Muchow

Reputation: 6711

var widthCalculator = (100 / breadcrumbCounter.length) * .01;

not

var widthCalculator = (100 / breadcrumbCounter.length) * 1%;

Cannot use % this way...

Upvotes: 3

Related Questions