0

I have a button that reacts to onclick() at the moment, but I want it to work with jquery.

$('#nupp1').click(function() {
    raiseButtonAction(realPlayer, gameState);
});

raisebuttonaction is in another .js This piece of code isn't working. Am I doing something wrong? edit1:

$(document).ready(function() {
    $('#nupp1').click(function() {
        raiseButtonAction(realPlayer, gameState);
    });
});
7
  • show the full code and markup, when you assign the handler? Commented Mar 28, 2011 at 18:05
  • Where did you put that piece of code? It should be in a "ready" handler. Commented Mar 28, 2011 at 18:05
  • Where are the variables realPlayer and gameState coming from? Commented Mar 28, 2011 at 18:05
  • Is raisebuttonaction script being included before this bind script? Commented Mar 28, 2011 at 18:06
  • Where/when is this code being run? Part of $(document.ready)? Commented Mar 28, 2011 at 18:06

1 Answer 1

3

Assuming you have this:

<head>
    <scripts />
</head>
<body>
    <a id="nupp1"></a>
</body>

You code will not work. jQuery will not be able to find the element. The element must exists for that function to work. So

<a id="nupp1"></a>
<script />

Will work because a is being rendered before the script.

You can also use $(document).ready or $() to execute your function when the DOM nodes load.

Or you can use jQuery.live.

jQuery API

  • ready Specify a function to execute when the DOM is fully loaded.
  • live Attach a handler to the event for all elements which match the current selector, now and in the future.
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.