0

I have checkboxes in a loop.

for example..

<% (1..5).each do |chk| %>
  <%= check_box_tag 'chk',"#{chk}", :checked=>false %>
<% end %>

I want to get the check box id's which are all checked.

Please help me to do that.

Thanks in advance.

5
  • Where do you want to get this value, on the client side or on the server side after form is submitted? Commented Apr 29, 2014 at 11:12
  • jquery method is fine. Commented Apr 29, 2014 at 11:14
  • Hey @poojaagarwal : Is this correct - for what you are looking ? Commented Apr 29, 2014 at 11:36
  • @Rubyist i want the checkbox ids like chk_1,chk_3 Commented Apr 29, 2014 at 11:37
  • have a look at updated answer .. Commented Apr 29, 2014 at 11:42

3 Answers 3

3

Jquery Link :

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

Try this:

HTML Code :

<input id="chk_1" name="foo" type="checkbox" value="1" />chk1
<input id="chk_2" name="foo" type="checkbox" value="2" />chk2
<input id="chk_3" name="foo" type="checkbox" value="3" />ckh3
<input id="chk_4" name="foo" type="checkbox" value="4" />chk4
<input id="chk_5" name="foo" type="checkbox" value="5" />chk5
<button>Alert List</button>

JsCode :

(function( $ ){
      $.fn.valList = function(){
            return $.map( this, function (elem) {
                  return elem.value || "";
            }).join( "," );
      };
      $.fn.idList = function(){
            return $.map( this, function (elem) {
                  return elem.id || "";
            }).join( "," );
      };
})( jQuery );

$('button').click(function(){
    alert($("input:checked").valList()); 
    alert($("input:checked").idList()); 

});

Running Example : JsFiddle Link

Sign up to request clarification or add additional context in comments.

6 Comments

$("input[type='checkbox']").val(); will only return the value of the first checked box.
@Rubyist which jquery should i use?
Jquery 1.11.0 with migrate file. Link is present in above answer.
if i use $("input[type='checkbox']").val(); its returning "on"
@BroiSatse : Buddy I have to take a look on your suggestion.
|
2

Try with:

<% (1..5).each do |chk| %>
   <%= check_box_tag 'chk[]',"#{chk}", :checked=>false%>
<% end %>

Then, in the controller, params[:chk] will give you an array of check box values which are checked.

Hope it helps :)

2 Comments

I want a jquery method.
That you must have said before in your question. Why downvoted?
1

Try This It may helps you

Ruby syntax:

 <% (1..5).each do |chk| %>
         <%= check_box_tag('chk[]', chk) %>
         <%= chk %>
      <% end %>

Jquery Syntax:

$('document').ready(function(){
    $('input["name=chk[]"]').change(function(){
     if($(this).attr("checked")){
    alert($(this).val())}
  })
})

which gives alert of the checked check box value when you change checkbox.

if you want checked check box value when document loads then try this

$('document').ready(function(){
           alert($('input["name=chk[]"]:checked').val())
   })

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.