1

I have few div elements and inside every div one checkbox. On click this div I want to check/uncheck checkbox and add class like active or checked. Have some code if you have any idea.

<div class="filter">
    <div id="select_10">
       <input type="checkbox" name="what" />
       visible1
    </div>
    <div id="select_91">
       <input type="checkbox" name="ever" />
       visible2
    </div>
    <div id="select_101">
       <input type="checkbox" name="whatever" />
       visible3
    </div>    
</div>

using this code:

$(document).on("click", "div.filter div", function (event) {
    var target = $(event.target);
    var id = $(this)attr('id');
    if (target.is('div#'+id+' input:checkbox')) {
        return;
    }
    var checkbox = $(this).find("input[type='checkbox']");
    checkbox.prop("checked", !checkbox.is(':checked'));
});
2
  • 1
    Perfect sunny time to use <label> instead of <div> Commented Mar 17, 2016 at 19:20
  • I will check - thanks :) Commented Mar 17, 2016 at 19:24

1 Answer 1

5

$(this)attr('id'); should have a . like in

$(this).attr('id');

That's it.


Why using DIV and JS when we have <label> for that purpose!

.filter label {
  cursor: pointer;
  display: block;
  background: #eee;
  margin:5px; padding: 10px;
}
<div class="filter">
  <label id="select_10">
    <input type="checkbox" name="what" />
    visible1
  </label>
  <label id="select_91">
    <input type="checkbox" name="ever" />
    visible2
  </label >
  <label id="select_101">
    <input type="checkbox" name="whatever" />
    visible3
  </label >    
</div>

all you need. Style label as you would for your DIV by just adding eventually display: block;


Or you can even move the inputs before the label and style the complete LABEL elements in pure CSS:

.filter input{
  position: absolute;
  visibility: hidden;
}

.filter label {
  cursor: pointer;
  display: block;
  margin:5px; padding: 10px;
  background: #eee;
}

.filter input:checked + label{
  background: #cf5;
}

.filter                 label:before{content: "\2610";}
.filter input:checked + label:before{content: "\2611";}
<div class="filter">
  
  <input  id="ckb_10" type="checkbox" name="what" />
  <label for="ckb_10">
    what
  </label>
  
  <input  id="ckb_91" type="checkbox" name="foo" />
  <label for="ckb_91">
    foo
  </label>
  
</div>

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

6 Comments

One dot and so much happiness
@TadeuszMajkowski hahahaaha I know happens to me all the time, but than I open Developer tools and I see my errors!
@TadeuszMajkowski strange my Netbeans would burn me alive if it noticed such error.
@TadeuszMajkowski you'e welcome. I've expanded my answer in the meantime, You might check the added content.
Ohhh You already add checked style - want to do it for myselft but I will check your solution :)
|

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.