0

i trying to change css using java script but it did not work.

function myFunction() {
    var x = document.getElementsByClassName('chirag');
    var check = document.getElementById('chk1');

    for (var i=0;i<x.length;i+=1){
	  if(check.checked){
    	   x.style.display = 'none';
	        }
	         else {
	        x.style.display = 'block';
	    }
	}
}
.chirag{
			display: none;
		}
<input type="checkbox"  id="chk1" value="Plate" onclick="myFunction()"> Plate  <br> 

<div id="plan_detail" class="chirag">
</div>

i try to add javascript function on checkbox event. how can i fix it and change the css property

2
  • 4
    x is a list of elements, not an element itself. Maybe you meant x[i].style.display ? Commented Oct 11, 2017 at 13:03
  • 1
    yes @JaredSmith thanks... it's work Commented Oct 11, 2017 at 13:10

3 Answers 3

2

You were missing the "i" on the for cycle:

function myFunction() {
    var x = document.getElementsByClassName('chirag');
    var check = document.getElementById('chk1');

    for (var i=0;i<x.length;i+=1){
	  if(!check.checked){
    	x[i].style.display = 'none';
	  } else {
	    x[i].style.display = 'block';
	  }
	}
}
.chirag{
			display: none;
		}
<input type="checkbox"  id="chk1" value="Plate" onclick="myFunction()"> Plate  <br> 

<div id="plan_detail" class="chirag">
   test
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

You have to include the integer "i" of the loop as index of the array of elements "x"

function myFunction() {
    var x = document.getElementsByClassName('chirag');
    var check = document.getElementById('chk1');

    for (var i=0;i<x.length;i+=1){
	  if(check.checked){
    	   x[i].style.display = 'none';
	        }
	         else {
	        x[i].style.display = 'block';
	    }
	}
}
.chirag{
			display: none;
		}
<input type="checkbox"  id="chk1" value="Plate" onclick="myFunction()"> Plate  <br> 

<div id="plan_detail" class="chirag">
</div>

Comments

0

javascript:

You miss i index:

function myFunction() {
    var x = document.getElementsByClassName('chirag');
    var check = document.getElementById('chk1');
    for (var i=0;i<x.length;i+=1) {
        if(check.checked)
           x[i].style.display = 'block';
        else 
            x[i].style.display = 'none';
    }
}

function myFunction() {
    var x = document.getElementsByClassName('chirag');
    var check = document.getElementById('chk1');
    for (var i=0;i<x.length;i+=1) {
        if(check.checked)
           x[i].style.display = 'block';
        else 
            x[i].style.display = 'none';
    }
}
.chirag {
    display: none;
}
<input type="checkbox"  id="chk1" value="Plate" onclick="myFunction()"> Plate  <br> 

<div id="plan_detail" class="chirag">
   Hello
</div>

With jquery:

$(document).ready(function(){
    $('#chk1').click(function(){
        $('.chirag').toggle();
    })
})
.chirag {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<input type="checkbox"  id="chk1" value="Plate"> Plate  <br> 

<div id="plan_detail" class="chirag">
   Hello
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.