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);
});
});
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);
});
});
Share
Improve this question
edited Mar 31, 2011 at 15:39
rosscj2533
9,3237 gold badges41 silver badges57 bronze badges
asked Mar 28, 2011 at 18:04
nilsnils
3353 gold badges5 silver badges16 bronze badges
7
- show the full code and markup, when you assign the handler? – vittore Commented Mar 28, 2011 at 18:05
- Where did you put that piece of code? It should be in a "ready" handler. – Pointy Commented Mar 28, 2011 at 18:05
-
Where are the variables
realPlayer
andgameState
ing from? – gen_Eric Commented Mar 28, 2011 at 18:05 -
Is
raisebuttonaction
script being included before this bind script? – BrunoLM Commented Mar 28, 2011 at 18:06 -
Where/when is this code being run? Part of
$(document.ready)
? – AlG Commented Mar 28, 2011 at 18:06
1 Answer
Reset to default 3Assuming 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.