Aaron Aaron - 17 days ago 5
Ajax Question

JQuery Id needs to be a variable

I am trying to use Ajax with Rails and JQuery. Is there a way to get the id # at the beginning of the JQuery function to be a variable. I have a checklist of checkboxes and want it so when the user checks on a checkbox the JQuery responds.
Here is my code

<script>
$(document).ready(function(){
if($("#item.id").is(':checked')) {
// how do I get the line above to work on every checkbox
alert('checked!');
} else {
alert('not checked!');
}
});
</script>



<%= link_to "Logout", root_path %>

<h1>Hello <%= @user.first_name%> <%= @user.last_name%></h1>

<%= form_for @item do |f| %>
<%= f.label :to_do %>:
<%= f.text_field :to_do %><br />
<%= f.hidden_field :email, :value => @user.email%>


<%= f.submit %>
<% end %>

<% @items.each do |item|%>
<%if item.email == @user.email%>
<%= form_for @item do |f| %>
<%= f.check_box :to_do, :id =>item.id, :value => item.id%>
//The line above gives the value to each checkbox
<%= item.to_do%><br />
<% end %>
<%end%>
<%end%>

Answer

make if($("#item.id").is(':checked')) {
as if($("#<%= @item.id %>").is(':checked')) {