0

i'm trying to refresh a javascript i created that set height on my div when page load.

<script>
    jQuery(function($) {
        $('#bloc2').height($('#zonetexteposition').height());
        $('#bloc2-vertical').height($('#zonetexteposition').height());
        $('#bloc2-horizontal').height($('#zonetexteposition').height());
    });
</script>

Until now, everything works fine. my divs are getting the right height from div #zonetexteposition.

The thing is, it calculate the height of the page only onload. My F.A.Q page have questions in it and the anwser appear on click of the question, so it change the height of my div #zonetexteposition. Since the javascript only run onload it doesnt resize to the new height of #zonetexteposition. So i would like to know if its possible to make a script that gonna refresh the above script onclick of a question.

Thanks, hope it's clear

3 Answers 3

1

Create a function that has the calculations:

function foo () {
    $('#bloc2').height($('#zonetexteposition').height());
    $('#bloc2-vertical').height($('#zonetexteposition').height());
    $('#bloc2-horizontal').height($('#zonetexteposition').height());
}

Add an event listener to the button:

$("button").on('click', function(event) {
   foo();
   //anything you want on the click also
});

Call the function on load:

jQuery(function($) {
    foo();
});
Sign up to request clarification or add additional context in comments.

2 Comments

when im creating the function like you did. it gives me an error Uncaught TypeError: $ is not a function
jQuery(function($) { function foo() {....}; $(button).on(...); foo(); }); put it like this
1

Wrap all the code you have done into a function, then, when page loads, call that function, whenever you click on the desired element, call the same function.

Comments

1

One quick answer would be : instead of calculating height everytime, just set the min-height instead of height on DOM load.

Like that :

$('#bloc2, #bloc2-vertical, #bloc2-horizontal').css('min-height',$('#zonetexteposition').height());

So you wouldn't have to recalculate height of the container after the DOM load, as the container would handle it "naturally" it it gets bigger.

Tout simplement :)

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.