core
core

Reputation: 33079

How to modify the CSS Properties (height width) of a div element via JQuery?

How come this doesn't create a vertical 5px-wide bar on the ride side of the window?

<html>
<head runat="server">
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">

        $(document).ready(function() {
            $("#scrollBar").css({
                height: $(window).height,
                width: 5,
                position: "absolute",                
                top: 0,
                left: $(window).width - 5,
                margin: 0
            });
        });

    </script>
</head>
<body>
    <div id="scrollBar" style="background-color: Red">test
    </div>
</body>
</html>

Upvotes: 2

Views: 1501

Answers (5)

Randell
Randell

Reputation: 6170

Use this instead:

I found several problems:

  • instead of $(window).height, use $(window).height()
  • instead of left: $(window).width - 5, use right: 0

    $(document).ready(function() {
        $("#scrollBar").css({
            height: $(window).height(),
            width: 5,
            position: "absolute",                
            top: 0,
            right: 0,
            margin: 0
        });
    });
    

Upvotes: 6

theIV
theIV

Reputation: 25774

Instead of left: $(window).width - 5, you could try right: 0.

EDIT: Yeah, that was pretty stupid. But, if I'm not mistaken, those should also be quoted, as in 'width': '5px',.

  $("#scrollBar").css({
    'height': $(window).height(),
    'width': '5px',
    'position': 'absolute',
    'top': 0,
    'right': 0,
    'margin': 0
  });

Upvotes: 0

Guilherme
Guilherme

Reputation: 561

From reading jQuery documentation you can see that you need to put the css properties and their values as string values.

 $("#scrollBar").css({
                'height': $(window).height(),
                'width': '5px',
                'position': 'absolute',                
                'top': '0',
                'left': $(window).width() - 5,
                'margin': '0'
            });

http://docs.jquery.com/CSS/css#properties

I'm not really sure tough how you would display those calculated values.

PS: Jonathas was right height and width are functions, not properties.

Upvotes: 1

John Boker
John Boker

Reputation: 83709

you might want to put "5px" instead of 5 for the width and $(window).height() + "px" for the height and ($(window).width() - 5) + "px" for the left.

Upvotes: 1

Sampson
Sampson

Reputation: 268344

Also note that jQuery's .width and .height should be .width() and .height().

Examples of use...

Upvotes: 1

Related Questions