0

I'm trying to add two variables together (two different heights) but for some odd reason even though they are both numbers, it is not adding them together and just refusing to do anything. How do I add two variables together?

HTML

<div id="imageSlider">
    <div id="imagesContainer">
        <div class="left" id="selectedImage">
            <div class="sliderImageAlign">
            </div>
        </div>
    </div>

    <div id="imagesUp">
        <div id="imagesArrowUp"></div>
    </div>

    <div id="imagesDown">
        <div id="imagesArrowDown"></div>
    </div>
</div>

jQuery

$(document).ready(function(){
    var imageHeight = $("#selectedImage").height(),
        containerHeight = $("#imagesContainer").height(),
        containerPos = $("#imagesContainer").position();

    $("#tellMeHeight").text(containerHeight / imageHeight);

    $("#imagesDown").click(function(){
        var containerNewPos = parseInt(containerPos + imageHeight);

        $("#imagesContainer").css({
            top: containerNewPos + 'px'
        });
    });
});
5
  • 2
    You should read about the methods you're using before you use them. .position() returns an object: api.jquery.com/position . So you can't exactly add something to it... Commented Jun 17, 2013 at 15:56
  • i'm storing the value of the position in the variable though? Commented Jun 17, 2013 at 15:58
  • 1
    BTW, to get an image's height, you have to be sure the image is loaded Commented Jun 17, 2013 at 15:59
  • 1
    containerPos contains the result of calling position(), so it is storing an object. If you want to do a calculation with it, use containerPos.left or containerPos.top to get the specific property Commented Jun 17, 2013 at 15:59
  • @roasted that won't be an issue, i measure the container for the image not the actual image because if i did it by the images height it would leave the slider being out of line. And oh i see now! I was trying to add to top and left (which isn't possible), easy enough mistake to make! Commented Jun 18, 2013 at 8:17

1 Answer 1

1

.position return's an object , you can call .top or .left on it

 containerPos = $("#imagesContainer").position().top;
Sign up to request clarification or add additional context in comments.

Comments

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.