Danisk
Danisk

Reputation: 123

HTML Radio Button Javascript

http://jsfiddle.net/8sbt5oua/8/

I would like to get my code only concentrated on a few everytime.
Instead of all of them, who could help me?

gif

    <input id="01" type="radio">
    <input id="02" type="radio">
    <input id="03" type="radio">
    <input id="04" type="radio">
    <input id="05" type="radio">
    <input id="06" type="radio">


var btn = document.getElementById("02");
setInterval(function() {
  var list=document.getElementsByTagName("input");
  for (i=0;i<list.length;i++) {
    btn = list[i];
    btn.checked == false ? btn.setAttribute("checked", "checked") : btn.removeAttribute("checked");
  }
}, 500);

Upvotes: 0

Views: 49

Answers (1)

A. L
A. L

Reputation: 12649

Here's the JsFiddle: http://jsfiddle.net/8sbt5oua/10/

First have two arrays that you want to flicker on and off.

var arr1 = [0,1,2];
var arr2 = [3,4,5];

You also want a global variable that toggles between the two.

var use_first = true;

Then you want to use the toggling variable to determine which one toggles on and which ones toggles off.

let remove;
let checked;

if (use_first)
{
    checked = arr1;
  remove = arr2;
}
else
{
    checked = arr2;
  remove = arr1;
}

And I think the foreach explains themselves since they're basically your code.

Edit:

remove.forEach(function(item, index)
{
    btn = list[item];
  btn.removeAttribute("checked");
});
checked.forEach(function(item, index)
{
    btn=list[item];
  btn.setAttribute("checked", "checked");
});

Upvotes: 1

Related Questions