jQuery fetch all checked checkboxes from a div through a click on button

Asked

Viewed 834 times

0

I need to get all the checkboxes CHECKED of a <div> in which it has another <div> daughter that this, possess a <input type="checkbox"> via jQuery.

Follows the structure:

<div class="metro perfil-filtro-expander-filtrados perfil-filtro-expander-overflow acoesFiltradas" id="acoesFiltradas">
     <--! item 1 -->
     <div style="padding-bottom: 10px;">
          <input id="@item.SelectedValue" type="checkbox" />
          <label for="@item.SelectedValue">
          <b>@item.Title</b>
          <br />
          <span class="perfil-filtro-expander-descricao">
               @item.Description
          </span>
          </label>
    </div>
    <--! item 2 -->
    <div style="padding-bottom: 10px;">
         <input id="@item.SelectedValue" type="checkbox" />
         <label for="@item.SelectedValue">
         <b>@item.Title</b>
         <br />
         <span class="perfil-filtro-expander-descricao">
              @item.Description
         </span>
         </label>
    </div>
    <--! item 3 -->
    <div style="padding-bottom: 10px;">
         <input id="@item.SelectedValue" type="checkbox" />
         <label for="@item.SelectedValue">
         <b>@item.Title</b>
         <br />
         <span class="perfil-filtro-expander-descricao">
               @item.Description
         </span>
         </label>
    </div>
</div>

jQuery:

$("#copiarParaTodasAcoes").click(function () {
    $(".acoesFiltradas :input:checked").each(function () {
        alert('teste');
    });
});

Knob:

<a href="">
   <img class="perfil-expander-botoes" id="copiarParaTodasAcoes" src="~/Images/tecbox/icons/expander-copy-icon.png" />
</a>
  • The error is in the way you are calling in jQuery $(".acoesFiltradas :input:checked") change to $(".acoesFiltradas input:checked")

  • no feedback.. the alert is not displayed yet.

  • I did not pay attention to all the code, analyzing better, I checked that the error is on your button, the image is inside a link that this with the link to the same page <a href="">, it tries to call javascript, but the page is already redirects at the same time. https://jsfiddle.net/dbk7a1os/, I changed the javascript and html of the button.

  • not working here, do not know why... Please enter the code based on my question as answer.

2 answers

2


You can change your button code and javascript by these:

Knob (change this in for id on link tag <a> and remove from image)

<a href="" id="copiarParaTodasAcoes">
   <img class="perfil-expander-botoes" src="~/Images/tecbox/icons/expander-copy-icon.png" />
</a>

Javascript: (added the preventDefault() to ignore the main link action and remove the ':' from the input selector)

$("#copiarParaTodasAcoes").click(function (e) {
    e.preventDefault();

    $(".acoesFiltradas input:checked").each(function () {
        alert('teste');
    });
});

The rest of the html remains the same. example link working

Note: I tested in Firefox and Chorme.

  • No answer yet... I don’t know why... I think that’s why I’m styling the checkbox as the following: http://answall.com/questions/80628/checkbox-style-style-stylio-metro check if this can affect, Please... check that the checkbox does not switch to true or false checked. Funny that in the tests here I put an Alert in the first line on Function and it is no longer displayed too.

  • Do you have an error in the browser console? if you can pass an homologation url to check it out

  • worse that it presents nothing... without any feedback! And also I do not have the Url... if you enter the link I mentioned, you will see the classes that edit my checkbox through the div with class 'metro'.

  • It’s something that is giving conflict, I played the css of the link you passed and worked right (only the first item is selected because they have the same id) - Link

  • truth, I’ll have to find out!

  • If you can’t, try climbing into some environment that I try to help.

  • Gee! It was the browser cash that wasn’t changing the saved version. I ran it in anonymous mode and it was first! Thanks!!!

  • how do I do the same but only for those not checked?

  • 1

    You can use the selector :not(), would look like this $(".acoesFiltradas input:not(:checked)")

  • Perfect! Thanks for the help.

  • Now inside the list, how do I access the properties of each div? I didn’t actually post here, but what I did, was one. each within another to make some comparisons, and then I need to access the data of each object of these "each’s", I know that through "this", I can get the one of each current, but I need to compare with each other each.

Show 6 more comments

1

Remove the element <a> that is encapsulating the element <img>, for this has no purpose. Unless you prefer to remove the element <img> and pass the image src as the element background <a>. In this case it will be necessary to use the following script: e.preventDefault();

$("#copiarParaTodasAcoes").on('click', function (e) {
    // caso seja utilizada a tag <a>: e.preventDefault();

    // o filtro para selecionar os inputs 'checked'
    // pode ser feito com o 'find' do jquery
    // $('.acoesFiltradas').find('input:checked')

    $(".acoesFiltradas input:checked").each(function () {
        alert(this.id);
    });
});
  • I received no reply either, please read my comment that this in the Jeferson’s reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.