0

I need a little help with setting "padding left" of <div id="menu"> to equal the width of <ol>

Here's an example code for reference

<ol style="width:80%">some code</ol>

<div id="menu">some text</div>

I'm trying to do it with jQuery, I don't know it well so this is my attempt

$("#menu").css({"paddingLeft": "$('ol').width()"});

It works only if I enter a value myself like 400px in here $("#menu").css({"paddingLeft": "400px"});

But this is for a responsive design and I want it to automaticaly get the value of <ol> element and in pixes

1
  • Thanks for the answers, it works. One thing though, when I re-size the browser it doesn't change the value to the new width. Only if I refresh. How do I make it update automatically? Commented Oct 11, 2014 at 5:24

4 Answers 4

1

Remove the " from "$('ol').width()" otherwise it will not work

$("#menu").css({"paddingLeft": $('ol').width()});

If you want to change the padding while changing the size then put it inside window resize() event

$(window).resize(function() {
    $("#menu").css({"paddingLeft": $('ol').width()});
});
Sign up to request clarification or add additional context in comments.

Comments

1

So close!

$("#menu").css({"paddingLeft": $('ol').width()});

Comments

1
$("#menu").css({"paddingLeft": $('ol').width()});

Comments

1

From jQuery API

you can check the method .css( propertyName, value ) and figure it out.

By the way, when you refresh your window size. code like this:

$(window).resize(function (){
   $("#menu").css({"paddingLeft": $('ol').width()});
})

1 Comment

That did it. Thank you for that last bit of help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.