Solved

toogling checkboxes with javascript

Posted on 2014-11-16
2
106 Views
Last Modified: 2014-11-16
Hi,

I have a view with two checkboxes like this:

                    <td style="width: 33%">
                        <input id="ppm" type="checkbox" checked />
                        <label for="ppm"><span></span>ppm</label>
                    </td>
                    <td style="width: 33%">
                        <input id="vol" type="checkbox" />
                        <label for="vol"><span></span>% vol</label>
                    </td>

Open in new window


They are styled using ccs3 like this:
input[type="checkbox"] {
    display:none;
}
input[type="checkbox"] + label span {
    display:inline-block;
    width:22px;
    height:19px;
    margin:-1px 4px 0 0;
    vertical-align:middle;
    background: url('../img/cbUnChecked.png') left top no-repeat;
    cursor:pointer;
}
input[type="checkbox"]:checked + label span {
    background: url('../img/cbChecked.png') left top no-repeat;
}

Open in new window


Now - when I check one of them I want to uncheck the other, i.e. I want to toggle their state. My approach was to use this code:
            $("#ppm").change(function () {
                var cb = $("#vol");
                console.log(cb);
                console.log(cb.checked);
                console.log(this.checked);
                if (this.checked) {
                    cb.checked = false;
                }
                else {
                    cb.checked = true;
                }
            });
            $("#vol").change(function () {
                var cb = $("#ppm");
                if (this.checked) {
                    cb.checked = false;
                }
                else {
                    cb.checked = true;
                }
            });

Open in new window


Here is the issue: cb is returned as an object but cb.checked is undefined.

What can I do to fix this - or, is there a better way to toggle the checkboxes?

Best regards
RTSol
0
Comment
Question by:RTSol
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40446159
Use radio button with the same name.
... else...
Test page : http://jsfiddle.net/evcedsLu/1/

            $("#ppm,#vol").change(function () {
                $(this).closest("tr").find(":checkbox").not(this).attr("checked", false);
            });

Open in new window

0
 

Author Closing Comment

by:RTSol
ID: 40446172
Thanks a lot!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now