0

Here i have one check box if that check box is checked then i want to display one div and if that checkbox is unchecked then i want to hide that div.

Below is my code its not working please help .

<div class="row1" id="homemove"> <span class="label">Home Move</span>
    <input type="checkbox" id="homecheck" onclick="HomeMove()" </input>
</div>  

function HomeMove() {
    if (document.SalesNew.HomeMove.checked) {
        document.getElementById("NewAddress").style.display = "block";
    } else if (!document.SalesNew.HomeMove.checked) {
        document.getElementById("NewAddress").style.display = "none";
    }
}
3
  • 1. SalesNew? What is that 2. Where is the NewAddress? Commented Jul 26, 2013 at 11:29
  • salesnew is the form id in which currnt div is present. New address is just other div below this it is working properly just issue is with checkbox . Commented Jul 26, 2013 at 11:32
  • If the form has an ID you should be using getElementById Commented Jul 26, 2013 at 11:33

4 Answers 4

2

Replace your Javascript HomeMove function as below

function HomeMove() {
    if (document.getElementById("homecheck").checked) {
        document.getElementById("NewAddress").style.display = "block";
    } else {
        document.getElementById("NewAddress").style.display = "none";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Not sure what document.SalesNew.HomeMove is supposed to be, but you should just create an event handler like so:

<div class="row1" id="homemove"> 
    <span class="label">Home Move</span>
    <input type="checkbox" id="homecheck"></input>
</div>

<script type="text/javascript">
    document.getElementById('homecheck').onchange = function() {
        document.getElementById("NewAddress").style.display = this.checked ? 'block' : 'none';
    }
</script>

Also, you don't have an element with the ID NewAddress, but I'm guessing there is one in the actual code you're using.

Comments

0

Or, if you want to keep your own code, you could do it this way:

http://jsbin.com/uludes/1/edit

Comments

0

Try out the following fiddle: http://jsfiddle.net/GtmWX/2/

Simple checkbox to show/hide div

HTML

<p>Check here to show div <input type="checkbox" name="checkbox" id="checkbox" tabindex="165" /></p>
<div id="hiddenDiv"></div>

JS

function setUp() {
document.getElementById("checkbox").onclick = function() {
    if (document.getElementById("checkbox").checked) {
        // show div
        document.getElementById("hiddenDiv").style.display = "block";
    } else {
        // hide div
        document.getElementById("hiddenDiv").style.display = "none";
    }
};
// hide on initial page load
document.getElementById("hiddenDiv").style.display = "none";
}

window.onload =  function() {
setUp();
};

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.