1

Need to know how I can use a Jquery var in a CSS property, I've searched around the net but haven't found any answers that refer to what I want, and also I'm not the greatest coder to figure this out. Have not used Jquery alot!

Code JQUERY:

 <script>
//find window height
          var winHeight = $(window).height(); 
          alert(winHeight);//=692
$('menuHolder').css(height(), winHeight);

 </script>
2
  • 3
    What does the function height return in .css(height(), winHeight); ? Have you actually read the .css documentation (I bet this would help you the most)? Also, as @Xander points out, in HTML there is no menuHolder element. Maybe you meant to use a class, or maybe you have to read some more jQuery tutorials first. Btw there is nothing like a "jQuery variable". jQuery is a library and JavaScript is the language. If any, it's a JavaScript variable, but since we know that it's about JavaScript, it's just a variable ;) Commented Apr 21, 2012 at 20:18
  • 3
    menuHolder is an html element?!?! ;) Commented Apr 21, 2012 at 20:19

2 Answers 2

2

There are many issues with your code:

  1. menuHolder is not a valid html tag.

  2. height() is a function call which I cannot imagine should ever return the string "height".

  3. It seems like you are not running this after the document is fully loaded?

Suppose your html looks like this:

<div id="menuHolder">This is the menu holder</div>

Note that the tag in question must have id attribute set to "menuHolder". If you have more than one menuHolder, then you should use the class attribute instead.

Then your js should be:

$(function(){
   $('#menuHolder').css("height", $(window).height());
});

http://jsfiddle.net/93hFW/

or

$(function(){
   $('#menuHolder').height($(window).height());
});

http://jsfiddle.net/93hFW/1/

If you have are using the class attribute instead as suggested above, then you should use a . (dot) instead of the # to prefix the jQuery selector.

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

5 Comments

Thanks for your reply, would i need to call that function or will it run automatically
@ScottRobertson The function is passed to jquery to be called once the DOM has fully loaded. Without this measure, jQuery may not be able to find the element, because it hasn't been loaded yet.
Hey thanks alot for your reply, I was using a class: this worked perfectly! $('.menuHolder').css("height", $(window).height());
@ScottRobertson, your welcome. jQuery selectors work like CSS selectors. If your question is answered, you should accept it, by clicking the check mark. Doing so will mark this question as answered, award you 2 reputation points and 15 reputation points to the person who has answered your question.
That would be you @-d_inevitable. Thanks.
2

You want to set the height of the element?

$('menuHolder').css("height", winHeight);

would be better to do

$('menuHolder').height(winHeight);

1 Comment

Code is full of <menuHolder /> elements for sure!

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.