I’m trying to basically increment and decrement a vote count when a button is pressed (I’ve dumbed down the situation to exploit my error); I bind two different buttons with click events;however, I fail to get any response out of them (tested with Firebug).
Jquery Code:
//This Code is located within Application.js (Rails)
$(document).ready(function() {
$('.buttonUp').bind('click', function() {
var voting_element = $(this).closest('.current_vote');
voting_element.text( Number(voting_element.text()) + 1 );
});
$('.buttonDown').bind('click', function() {
var voting_element = $(this).closest('.current_vote');
voting_element.text( Number(voting_element.text()) - 1 );
});
});
Each button with class buttonUp or buttonDown is located within a form containing the single submit button.
I am doing this in Rails 3.1.3 and using the following versions of the Jquery Library:
Jquery-Rails => 1.0.12
Jquery => 1.6.1
JqueryUI => 1.8.12
JqueryUJS => The one that comes with Jquery-Rails above.
Live doesn’t work either, I wasn’t expecting it would since I wait for the document to load before I bind the methods to the buttons.
Here is a snippet of the HTML in question.
<tr>
<td>pop</td>
<td>pop</td>
<td><a href="/posts/12">Show</a></td>
<td><a href="/posts/12/edit">Edit</a></td>
<td><a href="/posts/12" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a></td>
<td><p>
<b>votes: </b>
</p>
<p class="current_vote">
1
</p>
<form action="/posts/increaseVote?id=12" class="button_to" data-remote="true" data-type="json" method="post"><div><input class="buttonUp" type="submit" value="increaseVote" /><input name="authenticity_token" type="hidden" value="TxEGwONzSD+tnJ19iHMdGjuCBJMFoNJdECEspDtZxxY=" /></div></form>
<form action="/posts/decreaseVote?id=12" class="button_to" data-remote="true" data-type="json" method="post"><div><input class="buttonDown" type="submit" value="decreaseVote" /><input name="authenticity_token" type="hidden" value="TxEGwONzSD+tnJ19iHMdGjuCBJMFoNJdECEspDtZxxY=" /></div></form>
</tr>
To get pointed in the right direction, have a look at this DEMO.
Here’s the JS: