1

How do you change a global-variable inside a jquery function. If I use this code below it will display "0" and not "15".

<script type>
theValue=0;
      $(document).ready(function(){
    theValue=15;
     });
</script>

<script type="text/javascript">
    document.write(theValue)
</script>
1
  • 2
    that's because $(document).ready(...) fires after the document is ready so theValue hasn't changed yet when document.write(theValue) is executed. Commented Apr 27, 2011 at 10:25

1 Answer 1

2

Thats because when you write, the (document).ready hasn't fired yet. Example, notice it writes 0 but when you click the button its 15.

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.