/    Sign up×
Community /Pin to ProfileBookmark

2 and 3 checkboxes

pls kindly help me to solve this issue. I want a submit button to show when I checked 2 checkboxes and 3 checkboxes.
I used this to hide and show when 1 checkbox is checked
<script type=”text/javascript”>
$(document).ready(function(){
$(‘#flip-ch1’).hide();
$(‘#radio’).mouseup(function () {
$(‘#ch1’).toggle();
$(‘#flip-ch1’).toggle();

});
});

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 19.2020 — Clarification needed:

Can you show the HTML and CSS of your request?

I am having trouble picturing which is the radio button or the checkbox.

Which element is being toggled based upon the mouseup event?

😕
Copy linkTweet thisAlerts:
@VITSUSAMar 19.2020 — @irumale#1616344 Can you please elaborate what problem you are facing, which platform you are using and what is your goal?
Copy linkTweet thisAlerts:
@irumaleauthorMar 19.2020 — Below is my code for the checkboxes and it work fine, if am selecting any of the options.

my problem is that I WANT A BUTTON TO SHOW IF I CHECKED TWO OR ALL THE OPTIONS of the checkboxes.

thanks in anticipation

$(document).ready(function(){

$('#flip-ch1').hide();

$('#radio').mouseup(function () {

$('#ch1').toggle();

$('#flip-ch1').toggle();

});

});

</script>

<script type="text/javascript">

$(document).ready(function(){

$('#flip-ch2').hide();

$('#radio2').mouseup(function () {

$('#ch1').toggle();

$('#flip-ch2').toggle();

});

});

</script>

<script type="text/javascript">

$(document).ready(function(){

$('#flip-ch3').hide();

$('#radio3').mouseup(function () {

$('#ch1').toggle();

$('#flip-ch3').toggle();

});

});

</script>

<script type="text/javascript">

$(document).ready(function(){

$('#flip-ch4').hide();

$('#radio2', '#radio3').mouseup(function () {

$('#ch1').toggle();

$('#flip-ch4').toggle();

});

});

</script>

<center><div>

<h3> <input type="checkbox" name="radio" id="radio">

<label for="computer">Computer Department</label>

<input type="checkbox" name="radio2" id="radio2">

<label for="statistics">Statistics Department</label>

<input type="checkbox" name="radio3" id="radio3">

<label for="physic">Physic Department</label>

</h3>

<br>

<button class="btn_style" id="ch1">Please Check Any Of The Department</button>

<button class="btn_style" id="flip-ch1">Allocate For Only Computer</button>

<button class="btn_style" id="flip-ch2">Allocate For Only Statistic</button>

<button class="btn_style" id="flip-ch3">Allocate For Only Physics</button>
Copy linkTweet thisAlerts:
@JMRKERMar 19.2020 — I am a bit surprised that the code works, but it may be because I don't fully understand the desired results.


  • 1. There are no matching ID values for the label elements in the HTML section.

  • 2. You have 3 checkbox on the top row of your display, BUT you have named them as if they are radio button elements. Why?

  • 3. On the 2nd row of button elements you have 4 different labeled buttons. Without any CSS to help, is it your desire for only the first button to be visible and the remaining (flip-ch#) elements to be initially hidden in the display?

  • 4. You have only 3 flip-ch# elements, but you JS refers to 4 different flip-ch# elements. Why?


  • Are you trying to display some of the (supposedly hidden) buttons when some of the checkboxes are chosen?

    Did you intend the for the top row to actually be radio buttons and not checkboxes (they have different logical intents).

    I'm sorry I have not given you any replacement code yet, but it is because I don't understand the purpose.

    If I don't understand, then any suggestions I could give will be useless to you and me

    and will possibly only confuse the problem more.

    Can you provide a verbal description of what should be hidden and what should be displayed when the various checkbox and button elements are selected?

    Note that each individual JS section could be combined into one JS section with <script>... </script> tags

    Also note that making separate request for duplicate topics is confusing. I would suggest staying with one thread until the problem is resolved rather than posting to similar threads.
    Copy linkTweet thisAlerts:
    @JMRKERMar 19.2020 — While awaiting a reply, I took a guess (SWAG) as to your desired actions.

    It may not meet your request requirements, but you might be able to modify further.

    It does not use JQuery at all and it does not do anything but change the displays.

    It is plain JavaScript only.

    &lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;&lt;head&gt;&lt;title&gt; Checkbox Control &lt;/title&gt;<br/>
    &lt;meta charset="UTF-8"&gt;<br/>
    &lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;<br/>
    &lt;!-- From: <URL url="https://www.webdeveloper.com/d/389228-2-and-3-checkboxes/4">https://www.webdeveloper.com/d/389228-2-and-3-checkboxes/4</URL> --&gt;<br/>
    &lt;style&gt;<br/>
    h3 { display: inline-block; }<br/>
    .fsize { font-weight: normal; }<br/>
    .btn_style { display: none; }<br/>
    &lt;/style&gt;

    &lt;/head&gt;&lt;body&gt;<br/>
    &lt;div&gt;<br/>
    &lt;h3&gt; <br/>
    &lt;input type="radio" name="rbtn" id="computer"&gt; &lt;label for="computer"&gt;Computer Department&lt;/label&gt;<br/>
    &lt;input type="radio" name="rbtn" id="statistics"&gt; &lt;label for="statistics"&gt;Statistics Department&lt;/label&gt;<br/>
    &lt;input type="radio" name="rbtn" id="physic"&gt; &lt;label for="physic"&gt;Physic Department&lt;/label&gt;<br/>
    &lt;input type="radio" name="rbtn" id="resetOpts"&gt; &lt;label for="resetOpts" class="fsize"&gt;Reset&lt;/label&gt;<br/>
    &lt;/h3&gt;<br/>
    &lt;br&gt;

    &lt;button class="btn_style" id="flip-ch1"&gt;Allocate For Only Computer&lt;/button&gt;<br/>
    &lt;button class="btn_style" id="flip-ch2"&gt;Allocate For Only Statistic&lt;/button&gt;<br/>
    &lt;button class="btn_style" id="flip-ch3"&gt;Allocate For Only Physics&lt;/button&gt;<br/>
    &lt;/div&gt;

    &lt;script&gt;<br/>
    const doc = (IDS) =&gt; document.getElementById(IDS);

    function show(info) { // alert(info); <br/>
    btns = ['flip-ch1','flip-ch2','flip-ch3'];<br/>
    btns.forEach(btn =&gt; doc(btn).classList.add('btn_style') );<br/>
    switch (info) {<br/>
    case 'computer' : doc('flip-ch1').classList.toggle('btn_style');<br/>
    doc('flip-ch2').classList.toggle('btn_style');<br/>
    break;<br/>
    case 'statistics' : doc('flip-ch2').classList.toggle('btn_style');<br/>
    break;<br/>
    case 'physic' : doc('flip-ch3').classList.toggle('btn_style');<br/>
    doc('flip-ch2').classList.toggle('btn_style');<br/>
    break;<br/>
    case 'resetOpts' : break;<br/>
    }<br/>
    }

    function init() {<br/>
    var sel = document.querySelectorAll('input'); // alert(sel.length);<br/>
    for (var i=0; i&lt;sel.length; i++) {<br/>
    sel<I>[i].addEventListener( 'click', function() { show(this.id) } );<br/>
    }<br/>
    } init();<br/>
    <br/>
    &lt;/script&gt;<br/>
    <br/>
    &lt;/body&gt;&lt;/html&gt;<br/>
    Copy linkTweet thisAlerts:
    @irumaleauthorMar 20.2020 — thanks for your time.

    I want to have three A, B and C checkboxes, in which i checked A i want a button A to appear/display like checkboxes B and C.

    Also, I want a button to display if i checked both A and B checkboxes,

    And another button if all the ckr=eckboxes are checked

    thanks
    Copy linkTweet thisAlerts:
    @JMRKERMar 21.2020 — Can you create a logic diagram of the checkbox and button states of your problem?

    For example:

    Checkbox A set, then Button 1 and 2 are set

    Checkbox B set, then Button 2 is set

    Checkbox C set, then Button 1 and 3 are set

    Reset button clears all checkboxes and buttons to reset status

    In my last post you can change the radio buttons on the top row to checkboxes by changing to the following:



    &lt;input type="checkbox" name="rbtn" id="computer"&gt; &lt;label for="computer"&gt;Computer Department&lt;/label&gt;<br/>
    &lt;input type="checkbox" name="rbtn" id="statistics"&gt; &lt;label for="statistics"&gt;Statistics Department&lt;/label&gt;<br/>
    &lt;input type="checkbox" name="rbtn" id="physic"&gt; &lt;label for="physic"&gt;Physic Department&lt;/label&gt;<br/>
    &lt;input type="checkbox" name="rbtn" id="resetOpts"&gt; &lt;label for="resetOpts" class="fsize"&gt;Reset&lt;/label&gt;



    but the above needs additional changes to perform the resets and toggle the checkboxes. Can do this after I understand the requirements.
    Copy linkTweet thisAlerts:
    @irumaleauthorMar 21.2020 — in summary, what I need is that

    when I checked two checkboxes I want a button to display

    and

    when I checked three checkboxes I want a button to display
    Copy linkTweet thisAlerts:
    @JMRKERMar 22.2020 — In summary, this is what you get :)

    &lt;!DOCTYPE html&gt;&lt;html lang="en"&gt;&lt;head&gt;&lt;title&gt; CBox Control &lt;/title&gt;<br/>
    &lt;meta charset="UTF-8"&gt;<br/>
    &lt;meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/&gt;<br/>
    &lt;!-- For: <URL url="https://www.webdeveloper.com/d/389228-2-and-3-checkboxes/8">https://www.webdeveloper.com/d/389228-2-and-3-checkboxes/8</URL> --&gt;<br/>
    &lt;style&gt;<br/>
    .hide { display: none; }<br/>
    &lt;/style&gt;

    &lt;/head&gt;&lt;body&gt;

    &lt;fieldset id="fset2"&gt; &lt;legend for="fset2"&gt; Checkbox 2 &lt;/legend&gt;<br/>
    &lt;input type="checkbox" id="cb2A"&gt; &lt;label for="cb2A"&gt; 2A &lt;/label&gt;<br/>
    &lt;input type="checkbox" id="cb2B"&gt; &lt;label for="cb2B"&gt; 2B &lt;/label&gt;<br/>
    &lt;input type="button" id="btn2AB" class="hide" value="CBox 2A / 2B"&gt;<br/>
    &lt;/fieldset&gt;

    &lt;fieldset id="fset3"&gt; &lt;legend for="fset3"&gt; Checkbox 3 &lt;/legend&gt;<br/>
    &lt;input type="checkbox" id="cb3A"&gt; &lt;label for="cb3A"&gt; 3A &lt;/label&gt;<br/>
    &lt;input type="checkbox" id="cb3B"&gt; &lt;label for="cb3B"&gt; 3B &lt;/label&gt;<br/>
    &lt;input type="checkbox" id="cb3C"&gt; &lt;label for="cb3C"&gt; 3C &lt;/label&gt;<br/>
    &lt;input type="button" id="btn3ABC" class="hide" value="CBox 3A / 3B / 3C"&gt;<br/>
    &lt;/fieldset&gt;

    &lt;script&gt;<br/>
    const doc = (IDS) =&gt; document.getElementById(IDS);

    function showBtn2(ndx, info) { // just some function to do for testing // alert(ndx + ' : ' + info.id);<br/>
    if (doc('cb2A').checked &amp;&amp; doc('cb2B').checked)<br/>
    { doc('btn2AB').classList.remove('hide'); } else { doc('btn2AB').classList.add('hide'); } <br/>
    }<br/>
    function showBtn3(ndx, info) { // just some function to do for testing // alert(ndx + ' : ' + info.id);<br/>
    if (doc('cb3A').checked &amp;&amp; doc('cb3B').checked &amp;&amp; doc('cb3C').checked ) <br/>
    { doc('btn3ABC').classList.remove('hide'); } else { doc('btn3ABC').classList.add('hide'); } <br/>
    }

    function padHandlers(){<br/>
    var sel2 = document.querySelectorAll('#fset2 input[type="checkbox"'); // alert(sel2.length)<br/>
    // convert collection to array, then map each element to an event function<br/>
    [...sel2].map(function mapper(elm, index){<br/>
    elm.addEventListener(<br/>
    "click", <br/>
    function clicker(e){ showBtn2(index, this); }, // end clicker(e) function<br/>
    false <br/>
    ); //end addEventListener<br/>
    }); //end map() function

    <i> </i><CODE>var sel3 = document.querySelectorAll('#fset3 input[type="checkbox"'); // alert(sel3.length)</CODE>
    // convert collection to array, then map each element to an event function<br/>
    [...sel3].map(function mapper(elm, index){<br/>
    elm.addEventListener(<br/>
    "click", <br/>
    function clicker(e){ showBtn3(index, this); }, // end clicker(e) function<br/>
    false <br/>
    ); //end addEventListener<br/>
    }); //end map() function<br/>
    }<br/>
    padHandlers();<br/>
    &lt;/script&gt;

    &lt;/body&gt;&lt;/html&gt;<br/>


    For some reason unknown to me, the forum post of code

    loses all code formatting and indentation. I would fix that for readability.
    Copy linkTweet thisAlerts:
    @irumaleauthorMar 23.2020 — Thank you JMRKER, the code work for me but in the first section

    **<input type="checkbox" id="cb2A"> <label for="cb2A"> 2A </label>

    <input type="checkbox" id="cb2B"> <label for="cb2B"> 2B </label>

    <input type="button" id="btn2AB" class="hide" value="CBox 2A / 2B">**


    I want a restriction on the buttons so that **ONLY TWO CHECKBOXES CAN BE CHECKED AT SINGLE TIME.**

    with the one you help' with, **ALL THREE CHECKBOXES are showing buttons**

    Thanks in anticipation
    Copy linkTweet thisAlerts:
    @JMRKERMar 23.2020 — @irumale#1616535

    Your request doesn't make sense to me. 😕

    First section only has two checkboxes. To check both at a single time is impossible.

    One is checked and then another is checked to have both checked

    The only way I see it could be done is to have a single checkbox, then if so, a second CBox is un-needed!

    If the code is close to your needs, modify the display to show how the layout is to be.

    Make an attempt at the solution to your problem and then, perhaps, we can understand the specific needs.

    If this was a real job and you could not specify the requirements better than you have,

    I would have rejected the proposal much earlier. I could never satisfy you as an employer.


    I've run out of guesses.
    ×

    Success!

    Help @irumale spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...