2

Im trying to call a jQuery inside a javaScript function, but it doesnt work, what I am doing wrong?

<script type="text/javascript" language="javascript">

        function Test() {
            $(document).ready(function () {
                $("#pezz").animate({
                    left: '+=150px',
                    height: '+=20px',
                    width: '+=20px'
                });
            });
        }
    </script>

call from a method int the Bean

RequestContext.getCurrentInstance().execute("Test");

UPDATE:

As Gibberish suggested I change the Script to this:

<script type="text/javascript" language="javascript">
  $(document).ready(function () {
   Test();
  });

 function Test() {
$("#pezz").animate({
  left: '+=150px',
  height: '+=20px',
  width: '+=20px'
});
}
</script>

But Works only once, Its supposed to work every time some condition is reached in the method, what canI do to fix this?

2
  • 4
    This happens because by the time your code runs, document's ready event has already fired. Commented Jul 8, 2016 at 11:31
  • 3
    Remove document-ready event handler Commented Jul 8, 2016 at 11:32

1 Answer 1

1

Restructure like this:

<script type="text/javascript" language="javascript">
  $(document).ready(function () {
    Test();
  });

  function Test() {
    $("#pezz").animate({
      left: '+=150px',
      height: '+=20px',
      width: '+=20px'
    });
  }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, It works, but only one time, in the method every time the user gives the wrong answer the image move. what can I do to fix this?

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.