Solved

toogling checkboxes with javascript

Posted on 2014-11-16
2
103 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
Comment Utility
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
Comment Utility
Thanks a lot!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

744 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

9 Experts available now in Live!

Get 1:1 Help Now