views:

34

answers:

2

Hi, I am trying to find out how to get a jQuery script to work correctly when a click event is started, the script calculates the height of a container (this script appears at the start of the page on load and works 100%) but I am now trying to get it to work inside a .click event.

If anyone knows how I should achieve this I would be very grateful.

p.s. I have tried doing document ready; waiting for the DOM to load but no cigar.

$(document).ready(function() {
    $("#hidden4").click(function(event) {
        event.preventDefault();
        $("#hidden4-1").slideToggle();

        var classDown = $('#hidden4 div.down').attr('class');
        var classUp = $('#hidden4 div.up').attr('class');

        if(classDown == 'down') {
            $("#hidden4 div.down").attr("class","up");
        }
        else if(classUp == 'up') {
            $("#hidden4 div.up").attr("class","down"); 
        }
            // Attain the absolute height of the container id container and assign to a usable variable
            var height = $("#container").height();
            alert (height);
            // Use the previous variable, divide by 4 then round that up and multiply by 4 and assign to new variable
            var newHeight = Math.ceil(height / 4) * 4;

            // Create a new variable and add the string "center" to it
            var finalHeight = "center ";

            // Using the previous variable add to it using the first 2 variables subtracting to find the difference and add 2
            finalHeight += (newHeight - height)+2;

            // Using the previous variable add to it the string "px" for the css selector usage
            finalHeight += "px";

            // Update the CSS of the required element altering the background position with the final variable
            $(".contentFooter").css('background-position', finalHeight);
    });
});

Thank you in advance.

+2  A: 
Šime Vidas
I did sadly try that before posted on here, it doesnt work unforunately :( any other ideas?
Daniel Wrigley
Additional, I do already have the script as a function working in the ready() and click() functions of the site :(
Daniel Wrigley
What does the alert method alert?
Šime Vidas
the alert shows me what value of height the jquery took when the code was committed, that way I can see if it is grabbing the new height after the click event toggles a container down to show, thus setting a new height for #container.
Daniel Wrigley
Try alerting the finalHeight value right before you set it to the `.contentFooter` element(s). What's its value? btw, in JavaScript, we don't use the term "committed". JavaScript code is evaluated. Alternatively, you can say it is executed.
Šime Vidas
The alert function is only there for de-bugging, I should not of left it in when posting the original code. All I need is for the script that attains the height of #container to not get the height prior to the .slideToggle() function and to get the height of #container, after the .slideToggle() function has been fully executed. As of the moment it does the opposite, hopefully this clears things up for you, sorry if I have confused you prior to this.
Daniel Wrigley
If you want to read the height of #container AFTER the slideToggle() has completed, then read it inside the callback function of slideToggle(). I will update my answer to demonstrate this ...
Šime Vidas
Also, I believe using camelCase is preferred when setting CSS properties via css(). Use `backgroundPosition` instead of `background-position`
Šime Vidas
That worked a charm Šime, thank you for remaining patient when others didnt and finding a resolution to the problem. :D
Daniel Wrigley
A: 

I don't think you need to nest another $(document).ready() in a jquery click, since the DOM is already ready when you applied the click event.

Shaoz
Sadly this was not the problem, although wrong and only left in due to testing, removing it has not fixed the problem
Daniel Wrigley