Aaron Aaron - 1 year ago 62
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

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

<%= 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 %>

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download