Home > OS >  by changing the selection of radio button now working fine
by changing the selection of radio button now working fine

Time:02-15

if first time I want to select or changing the radio button I want to change the label color to red, but it not working perfectly, I use a on change function inside radio button

<script>
    function changecolor(){
      document.getElementsByClassName("label").style.color='red'
    }
  </script>
<body>
<div >
  <label for="one" >Select Any</label>
    <div >
     <input id="one"  name="radio-group" type="radio" onchange="changecolor()" >
      <input id="one"  name="radio-group" type="radio" onchange="changecolor()">
   </div>
</div>
</body>
</html>

CodePudding user response:

document.getElementsByClassName return an htmlcollection so you have two solutions to make work your code

  • iterate on each element of the htmlcollection and apply the style
  • or use an index for access one of the specific element

function changecolor(){
  document.getElementsByClassName("label")[0].style.color='red'
}
<div >
  <label for="one" >Select Any</label>
    <div >
     <input id="one"  name="radio-group" type="radio" onchange="changecolor()" >
      <input id="one"  name="radio-group" type="radio" onchange="changecolor()">
   </div>
</div>

  • Related