0

I am looking to obtain the width of each <li> within the main navigation bar which can be obtained from the main LI. Whilst the below code works it seems to apply the same width to the following LI's rather than detecting it's own individual size.

How would I go about making this dynamic please?

$('.navbar-nav-middle li').width($('.navbar-nav-middle a').parent().width());
1
  • Can you post your HTML as well? Commented Jan 4, 2017 at 10:21

1 Answer 1

2

You need to use contextual this enclosed with a function here:

$('.navbar-nav-middle li').width(function () {
  return $(this).find("a").parent().width()
});

This might still give the same thing, as <li> takes up its content's width.

Sign up to request clarification or add additional context in comments.

4 Comments

This is fantastic, thank you very much. Worked a treat!
@Bradley Glad to help mate.
I'd like to ask, if the user was to resize the screen viewport if we could have this redetect the new LI width if at all possible? as when it hits certain breakpoints the LI may have a smaller font size setting so the width might be slightly less?
@Bradley You can detect the resize of window: $(window).resize(function () { ... })... Using @media queries is the best for this.

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.