Solved

Select only one checkbox in a group

Posted on 2016-09-04
13
64 Views
Last Modified: 2016-09-04
I have a series of checkboxes to use instead of radios (radios are not available to the system).

I cobbled together this jquery to enable a psudeo effect, however its a little dodgy, and I'm no jquery expert.

This uses an initial checkbox to query against and what I did was hide it using css. There has to be a more eloquent way of doing this, without the need of the first checkbox.

here is the code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('.compbox :checkbox').not(".hidebox").click(function () {
   var exclude = "";
 $('.compbox :checkbox').not($(this).add(exclude)).removeAttr('checked');
});
});
</script>
<style>
.hidebox {display:none;}
.compbox {display:inline-block;}
</style>


<div class="compbox">
    <input type="checkbox" class="hidebox" />
    <input type="checkbox" class="compbox" /> AAA 
    <input type="checkbox" class="compbox" /> BBB
    <input type="checkbox" class="compbox" /> CCC
    <input type="checkbox" class="compbox" /> DDD
    <input type="checkbox" class="compbox" /> EEE
    <input type="checkbox" class="compbox" /> FFF
</div>

Open in new window

Ideally I would like this to not be reliant upon the first checkbox and work without the need for an extra hidden one.

Possible?
0
Comment
Question by:Daryl Isaacs
  • 6
  • 6
13 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41783580
You can apply radio behavior to checkboxes this way:
$('.compbox :checkbox').click(function(){
        $('.compbox :checkbox').prop('checked', false);
        $(this).prop('checked', true);
});

Open in new window


In the click event, you first uncheck all checkboxes in the group, then you set property checked to true for the one clicked using $(this).

You can see it in action here: http://test.webintenerife.com/checkbox-as-radio.php

And this is the full page:
<div class="compbox">
	<input type="checkbox" class="compbox" /> AAA 
	<input type="checkbox" class="compbox" /> BBB
	<input type="checkbox" class="compbox" /> CCC
	<input type="checkbox" class="compbox" /> DDD
	<input type="checkbox" class="compbox" /> EEE
	<input type="checkbox" class="compbox" /> FFF
</div>
<script src="js/jquery-1.11.3.min.js"></script>
<script>
	$(document).ready(function () {
		$('.compbox :checkbox').click(function () {
			$('.compbox :checkbox').prop('checked', false);
			$(this).prop('checked', true);
		});
	});
</script>

Open in new window

0
 

Author Comment

by:Daryl Isaacs
ID: 41783605
Whoa that worked well in the test page - Ill add it to the environment and lets see


thank you - keep you appraised
0
 

Author Comment

by:Daryl Isaacs
ID: 41783619
Well I dumped it into the js of the system and it doesnt do what it does on a page

the checkboxes themsellves are from an array with two classes on it
one (hideme)
to hide the content of the array from showing in the admin section and

two
(compbox)
to control the look and feel of the checkboxes in the admin area

here is the array
array(
                'type' => 'checkbox',
                "holder" => "div",
                "class" => "compbox",              
                "class" => "hideme",
                "heading" => __("<div class='division_a'></div>
                  <span class='title_a'>Complimentary Sides</span>
                    <span style='color:#666;'>Side dishes to compliment this selection. Leave choices unchecked for no sides.</span>
                    <hr class='gspc'><br />", 'rbm_menu_item'),
                'param_name' => 'comes_with',
                'value' => array(
                        __( ' Coffee', 'rbm_menu_item' ) => ' Coffee',
                        __( ' Eggs', 'rbm_menu_item' ) => ' Eggs',
                        __( ' Toast', 'rbm_menu_item' ) => ' Toast',
                        __( ' English Muffin', 'rbm_menu_item' ) => ' English Muffin',

                    ),

Open in new window

Thats about as clear as mud, right? :)
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783635
If your html is as the one posted above it shoulkd just work (checkbox with class 'hideme' doesn't matter: see my updated example). If it does not this means we have some confilct somewhere: can you share a live link to the page? Or/and the original full code? for the page? Since we use jQuery, we act on the client side so It is not important how the page is built on server side: what's matter is the final output, that is the markup and some other javascript eventually oaded which could conflict with my jquery.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41783649
radios are not available to the system
What does this mean?

Might be helpful for background information on how checkboxes and radios work...
https://www.experts-exchange.com/articles/5450/Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
0
 

Author Comment

by:Daryl Isaacs
ID: 41783663
The reason radios dont work is that this is for a Visual Composer plugin and the VC library apparently does not support them unless a lot of extra code is thrown at it. (which I dont know how to do)

The file is attached the only call to js is from this:
wp_enqueue_script( 'rbm_menu_js', plugins_url('assets/js/rbm_menu.js', __FILE__), array('jquery') );

Open in new window

and all that file contains is this
//alert("rbm_menu.js is loaded in admin");

Open in new window

I put that alert there to make sure it was active...

here is the file in question - please ignore all my comments inside
robab_menu.php
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783672
So you are developing a Wordpress plugin. And where is the code which should make checkboxes behave as radiobuttons?
0
 

Author Comment

by:Daryl Isaacs
ID: 41783694
I added what you sent to the rbm_menu.js file, located in the assets/js dir.

Pasted it in as is, then ran the plugin with the alert activated - the alert worked, the rest no.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783723
I see. Unfortunately, I don't know anything bout wp plugins development and I can't guess why a perfectly working code should stop to work once inserted in a wp plugin.
I'll request attention to a moderator in order to add wordpress topic: some wp expert will can help you to go forward.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783813
Hi. How did you manage to solve the issue?
0
 

Author Comment

by:Daryl Isaacs
ID: 41783912
I didnt, I put it on the backburner to get some other css things done - I'll have to return to it later and readdress the issue.

Meanwhile I have another issue with separating the arrays into a separate file so editing is easier. trying to figure a way to have it so its in a dir of its own. If you look at the file I sent you'll see just how big its getting and I want to break it into smaller chunks (just dont know how) :)
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41783925
It doesn't look so big: 722 lines of code (blank lines and comments included!). But what you're trying to do refers to the internal logic of the plugin: I suggest you to post a new question adding Wordpress  to the Topic list.
0
 

Author Comment

by:Daryl Isaacs
ID: 41783953
It appears long to me (specially when I have to scroll up and down to find a pertinent part - thank goodness for sublime text)

I just don't want to be annoying to people, posting a lot of questions and getting them all mad - so I try to keep most of what I'm doing in small bites and try my best to keep it centered to what I originally asked.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 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