How to select only two consecutive checkboxes at a time using Javascript











up vote
0
down vote

favorite
2












In html, I have four checkboxes 1,2,3,4. I want to select 1 and 2 at a time, when I select 3, 1 should be cleared automatic, When I select 4, 2 should be cleared (now 3 and 4 are selected, 1 and 2 are not), also I should not select 1 and 3, 1 and 4, 2 and 4, vice versa, only consecutive numbered checkboxes i.e 1 and 2, 2 and 3, 3 and 4, **but not 1 and



edit-1



Javascript



function callme() {
var cbcb1 = document.getElementById("cb1");
var cbcb2 = document.getElementById("cb2");
var cbcb3 = document.getElementById("cb3");
var cbcb4 = document.getElementById("cb4");
if(cbcb1.checked && cbcb2.checked){
if(cbcb3.checked)
cbcb1.checked = false;
if(cbcb4.checked)
cbcb4.checked = false;
}
if(cbcb2.checked && cbcb3.checked){
if(cbcb4.checked)
cbcb2.checked = false;
if(cbcb1.checked)
cbcb3.checked = false;
}
if(cbcb3.checked && cbcb4.checked){
if(cbcb2.checked)
cbcb4.checked = false;
if(cbcb1.checked)
cbcb1.checked = false;
}
}


edit 2:



cb1 = id of checkbox 1



cb2 = id of checkbox 2



cb3 = id of checkbox 3



cb4 = id of checkbox 4



My Logic: The above javascript code works when selected 1 & 2, if 3 selected, 1 is unchecked, and now if 4 selected, 2 is unchecked. But now if I select 2 (when 3 and 4 are selected), 2 is not selected, I have to uncheck 4, then select 2.










share|improve this question




















  • 1




    You can do all that by identifying your checkboxes with IDs, storing them in independent variable and then applying all the rules that you described.
    – Davo
    Nov 10 at 12:45






  • 1




    On StackOverflow, we help with code problems. That means you must post the code you have so far, describe what exactly goes wrong, and what is expected instead.
    – connexo
    Nov 10 at 12:50










  • oh, code, so editing
    – Rishabh
    Nov 10 at 13:16















up vote
0
down vote

favorite
2












In html, I have four checkboxes 1,2,3,4. I want to select 1 and 2 at a time, when I select 3, 1 should be cleared automatic, When I select 4, 2 should be cleared (now 3 and 4 are selected, 1 and 2 are not), also I should not select 1 and 3, 1 and 4, 2 and 4, vice versa, only consecutive numbered checkboxes i.e 1 and 2, 2 and 3, 3 and 4, **but not 1 and



edit-1



Javascript



function callme() {
var cbcb1 = document.getElementById("cb1");
var cbcb2 = document.getElementById("cb2");
var cbcb3 = document.getElementById("cb3");
var cbcb4 = document.getElementById("cb4");
if(cbcb1.checked && cbcb2.checked){
if(cbcb3.checked)
cbcb1.checked = false;
if(cbcb4.checked)
cbcb4.checked = false;
}
if(cbcb2.checked && cbcb3.checked){
if(cbcb4.checked)
cbcb2.checked = false;
if(cbcb1.checked)
cbcb3.checked = false;
}
if(cbcb3.checked && cbcb4.checked){
if(cbcb2.checked)
cbcb4.checked = false;
if(cbcb1.checked)
cbcb1.checked = false;
}
}


edit 2:



cb1 = id of checkbox 1



cb2 = id of checkbox 2



cb3 = id of checkbox 3



cb4 = id of checkbox 4



My Logic: The above javascript code works when selected 1 & 2, if 3 selected, 1 is unchecked, and now if 4 selected, 2 is unchecked. But now if I select 2 (when 3 and 4 are selected), 2 is not selected, I have to uncheck 4, then select 2.










share|improve this question




















  • 1




    You can do all that by identifying your checkboxes with IDs, storing them in independent variable and then applying all the rules that you described.
    – Davo
    Nov 10 at 12:45






  • 1




    On StackOverflow, we help with code problems. That means you must post the code you have so far, describe what exactly goes wrong, and what is expected instead.
    – connexo
    Nov 10 at 12:50










  • oh, code, so editing
    – Rishabh
    Nov 10 at 13:16













up vote
0
down vote

favorite
2









up vote
0
down vote

favorite
2






2





In html, I have four checkboxes 1,2,3,4. I want to select 1 and 2 at a time, when I select 3, 1 should be cleared automatic, When I select 4, 2 should be cleared (now 3 and 4 are selected, 1 and 2 are not), also I should not select 1 and 3, 1 and 4, 2 and 4, vice versa, only consecutive numbered checkboxes i.e 1 and 2, 2 and 3, 3 and 4, **but not 1 and



edit-1



Javascript



function callme() {
var cbcb1 = document.getElementById("cb1");
var cbcb2 = document.getElementById("cb2");
var cbcb3 = document.getElementById("cb3");
var cbcb4 = document.getElementById("cb4");
if(cbcb1.checked && cbcb2.checked){
if(cbcb3.checked)
cbcb1.checked = false;
if(cbcb4.checked)
cbcb4.checked = false;
}
if(cbcb2.checked && cbcb3.checked){
if(cbcb4.checked)
cbcb2.checked = false;
if(cbcb1.checked)
cbcb3.checked = false;
}
if(cbcb3.checked && cbcb4.checked){
if(cbcb2.checked)
cbcb4.checked = false;
if(cbcb1.checked)
cbcb1.checked = false;
}
}


edit 2:



cb1 = id of checkbox 1



cb2 = id of checkbox 2



cb3 = id of checkbox 3



cb4 = id of checkbox 4



My Logic: The above javascript code works when selected 1 & 2, if 3 selected, 1 is unchecked, and now if 4 selected, 2 is unchecked. But now if I select 2 (when 3 and 4 are selected), 2 is not selected, I have to uncheck 4, then select 2.










share|improve this question















In html, I have four checkboxes 1,2,3,4. I want to select 1 and 2 at a time, when I select 3, 1 should be cleared automatic, When I select 4, 2 should be cleared (now 3 and 4 are selected, 1 and 2 are not), also I should not select 1 and 3, 1 and 4, 2 and 4, vice versa, only consecutive numbered checkboxes i.e 1 and 2, 2 and 3, 3 and 4, **but not 1 and



edit-1



Javascript



function callme() {
var cbcb1 = document.getElementById("cb1");
var cbcb2 = document.getElementById("cb2");
var cbcb3 = document.getElementById("cb3");
var cbcb4 = document.getElementById("cb4");
if(cbcb1.checked && cbcb2.checked){
if(cbcb3.checked)
cbcb1.checked = false;
if(cbcb4.checked)
cbcb4.checked = false;
}
if(cbcb2.checked && cbcb3.checked){
if(cbcb4.checked)
cbcb2.checked = false;
if(cbcb1.checked)
cbcb3.checked = false;
}
if(cbcb3.checked && cbcb4.checked){
if(cbcb2.checked)
cbcb4.checked = false;
if(cbcb1.checked)
cbcb1.checked = false;
}
}


edit 2:



cb1 = id of checkbox 1



cb2 = id of checkbox 2



cb3 = id of checkbox 3



cb4 = id of checkbox 4



My Logic: The above javascript code works when selected 1 & 2, if 3 selected, 1 is unchecked, and now if 4 selected, 2 is unchecked. But now if I select 2 (when 3 and 4 are selected), 2 is not selected, I have to uncheck 4, then select 2.







html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 13:27

























asked Nov 10 at 12:07









Rishabh

63




63








  • 1




    You can do all that by identifying your checkboxes with IDs, storing them in independent variable and then applying all the rules that you described.
    – Davo
    Nov 10 at 12:45






  • 1




    On StackOverflow, we help with code problems. That means you must post the code you have so far, describe what exactly goes wrong, and what is expected instead.
    – connexo
    Nov 10 at 12:50










  • oh, code, so editing
    – Rishabh
    Nov 10 at 13:16














  • 1




    You can do all that by identifying your checkboxes with IDs, storing them in independent variable and then applying all the rules that you described.
    – Davo
    Nov 10 at 12:45






  • 1




    On StackOverflow, we help with code problems. That means you must post the code you have so far, describe what exactly goes wrong, and what is expected instead.
    – connexo
    Nov 10 at 12:50










  • oh, code, so editing
    – Rishabh
    Nov 10 at 13:16








1




1




You can do all that by identifying your checkboxes with IDs, storing them in independent variable and then applying all the rules that you described.
– Davo
Nov 10 at 12:45




You can do all that by identifying your checkboxes with IDs, storing them in independent variable and then applying all the rules that you described.
– Davo
Nov 10 at 12:45




1




1




On StackOverflow, we help with code problems. That means you must post the code you have so far, describe what exactly goes wrong, and what is expected instead.
– connexo
Nov 10 at 12:50




On StackOverflow, we help with code problems. That means you must post the code you have so far, describe what exactly goes wrong, and what is expected instead.
– connexo
Nov 10 at 12:50












oh, code, so editing
– Rishabh
Nov 10 at 13:16




oh, code, so editing
– Rishabh
Nov 10 at 13:16












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Did you want something like this?






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





Here's a working example.



On the other hand, I'm pretty sure there's a better way to do it (using loops). I've came up with one. But, I haven't been able to figure out the last case (1&4)






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>








share|improve this answer





















  • oh yes! this is what I wanted, Thanks
    – Rishabh
    Nov 10 at 14:17






  • 1




    Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
    – tevemadar
    Nov 10 at 14:19












  • Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
    – C.RaysOfTheSun
    Nov 10 at 14:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238785%2fhow-to-select-only-two-consecutive-checkboxes-at-a-time-using-javascript%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Did you want something like this?






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





Here's a working example.



On the other hand, I'm pretty sure there's a better way to do it (using loops). I've came up with one. But, I haven't been able to figure out the last case (1&4)






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>








share|improve this answer





















  • oh yes! this is what I wanted, Thanks
    – Rishabh
    Nov 10 at 14:17






  • 1




    Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
    – tevemadar
    Nov 10 at 14:19












  • Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
    – C.RaysOfTheSun
    Nov 10 at 14:21















up vote
1
down vote



accepted










Did you want something like this?






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





Here's a working example.



On the other hand, I'm pretty sure there's a better way to do it (using loops). I've came up with one. But, I haven't been able to figure out the last case (1&4)






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>








share|improve this answer





















  • oh yes! this is what I wanted, Thanks
    – Rishabh
    Nov 10 at 14:17






  • 1




    Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
    – tevemadar
    Nov 10 at 14:19












  • Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
    – C.RaysOfTheSun
    Nov 10 at 14:21













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Did you want something like this?






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





Here's a working example.



On the other hand, I'm pretty sure there's a better way to do it (using loops). I've came up with one. But, I haven't been able to figure out the last case (1&4)






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>








share|improve this answer












Did you want something like this?






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





Here's a working example.



On the other hand, I'm pretty sure there's a better way to do it (using loops). I've came up with one. But, I haven't been able to figure out the last case (1&4)






window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>








window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
checks[0].addEventListener('change', function(){
if(this.checked){
checks[2].checked = false;
checks[3].checked = false;
}
});
checks[1].addEventListener('change', function(){
if(this.checked){
checks[3].checked = false;
}
});
checks[2].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
}
});
checks[3].addEventListener('change', function(){
if(this.checked){
checks[0].checked = false;
checks[1].checked = false;
}
});
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>





window.onload = ()=>{
let checks = document.querySelectorAll('input[type=checkbox]');
for(let i = 0; i < checks.length; i++){
if(i <= 1){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i+2].checked = false;
}
});
}
else if(i >= 2 && i <= 3){
checks[i].addEventListener('change', function(){
if(this.checked){
checks[i-2].checked = false;
}
});
}
}
}

<input type="checkbox" class="checks" id="check1">
<label for="check1">Check 1</label>
<input type="checkbox" class="checks" id="check2">
<label for="check2">Check 2</label>
<input type="checkbox" class="checks" id="check3">
<label for="check3">Check 3</label>
<input type="checkbox" class="checks" id="check4">
<label for="check4">Check 4</label>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 14:14









C.RaysOfTheSun

37617




37617












  • oh yes! this is what I wanted, Thanks
    – Rishabh
    Nov 10 at 14:17






  • 1




    Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
    – tevemadar
    Nov 10 at 14:19












  • Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
    – C.RaysOfTheSun
    Nov 10 at 14:21


















  • oh yes! this is what I wanted, Thanks
    – Rishabh
    Nov 10 at 14:17






  • 1




    Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
    – tevemadar
    Nov 10 at 14:19












  • Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
    – C.RaysOfTheSun
    Nov 10 at 14:21
















oh yes! this is what I wanted, Thanks
– Rishabh
Nov 10 at 14:17




oh yes! this is what I wanted, Thanks
– Rishabh
Nov 10 at 14:17




1




1




Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
– tevemadar
Nov 10 at 14:19






Tip for the first one: you can use the id-s as variables, so checks[0].something is just the same as check1.something. "id-d" elements become implicit variables in browser context.
– tevemadar
Nov 10 at 14:19














Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
– C.RaysOfTheSun
Nov 10 at 14:21




Thanks for the tip, @tevemadar! This is what I like about SO, I learn something new every time :D
– C.RaysOfTheSun
Nov 10 at 14:21


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238785%2fhow-to-select-only-two-consecutive-checkboxes-at-a-time-using-javascript%23new-answer', 'question_page');
}
);

Post as a guest