1

I have the following function that I want to run on page load and re-run when you click div#trigger. When you click div#trigger everything is fine. However on page load the code appears to sometimes work but sometimes indent the the wrong amount, and the alert (which i just put in there for debugging purposes) never fires. Thanks

  function textIndentFunc () {

     textString = $('#from-1 :selected').text();

     $('#hidden').text(textString);

     textWidth = $('#hidden').width();

     inputWidth = $('#from-1 select').width();

     indentMy = (inputWidth / 2) - (textWidth / 2);

    $('#from-1').css('text-indent',indentMy);

}

 $('#trigger').click(function(){
    textIndentFunc();
    alert('fire');
 });

textIndentFunc();

UPDATE - I should have mentioned this code runs on document.ready.

UPDATE - moving the code to window.load doesn't change anything.

1
  • Can you post what you have on a jsFiddle? Commented May 14, 2012 at 16:10

5 Answers 5

2

Wrap the function call in a document.ready handler like this...

$(function() {
    textIndentFunc();
});

That makes sure that the DOM is loaded before trying to do anything to manipulate it, otherwise you'll get different results at different times, depending on what's loaded and what's not.

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

2 Comments

It was already in a function but ive now moved it to window.load but the problem persists. Thanks
Seeing the rest of your code would help then because what you've shown us should work fine.
2

Your function need to be called in document.ready call

$(document).ready(function() {
   textIndentFunc();
});

Comments

1

Try as below

  $(document).ready(function () {
$('#trigger').click(function(){
    textIndentFunc();
    alert('fire');
 });
});

function textIndentFunc () {

     textString = $('#from-1 :selected').text();

     $('#hidden').text(textString);

     textWidth = $('#hidden').width();

     inputWidth = $('#from-1 select').width();

     indentMy = (inputWidth / 2) - (textWidth / 2);

    $('#from-1').css('text-indent',indentMy);

}
textIndentFunc();
<div id="trigger">Hai</div>

Comments

0

Do you have another function that also runs on page load? Because if this is the case maybe the 2 functions have a conflict...

2 Comments

Only other function hides the browser chrome: function hideChrome() { setTimeout(function() { window.scrollTo(0, 1) }, 100); }
Im not sure if moving the code to window.load helped but for some reason when I also changed the order of my other function it fixed this. Thanks
0

If you already are calling textIndendFunc in $(document).ready and is still not getting what you're expecting, try moving textIndentFunc to $(window).load.

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.