AnankumarAadhi Posted August 16, 2018 Share Posted August 16, 2018 <input type="checkbox" id="myCheck" onclick="myFunction()"><h4> Live Single</h4> <p id="area" style="display:none">Sample</p> <input type="checkbox" id="myCheck" onclick="myFunction()"><h4> Live Corporate</h4> <p id="area" style="display:none"><span>$350.00</span> </p> <script> function myFunction() { var checkBox = document.getElementById("myCheckk"); var text = document.getElementById("textx"); if (checkBox.checked == true) { text.style.display = "block"; } else { text.style.display = "none"; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/ Share on other sites More sharing options...
requinix Posted August 16, 2018 Share Posted August 16, 2018 And? Are you having problems with it? What does your browser's error console say? Have you checked that you named everything correctly? Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/#findComment-1560363 Share on other sites More sharing options...
AnankumarAadhi Posted August 16, 2018 Author Share Posted August 16, 2018 26 minutes ago, requinix said: And? Are you having problems with it? What does your browser's error console say? Have you checked that you named everything correctly? Hi . Thanks for Reply the problem was only one check box working. Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/#findComment-1560364 Share on other sites More sharing options...
AnankumarAadhi Posted August 16, 2018 Author Share Posted August 16, 2018 <div class="checkboxwrap"> <input type="checkbox" id="myCheck" onclick="myFunction()"><h4> Live Single</h4></div> <p id="text" style="display:none">Checkbox is CHECKED!</p> <div class="checkboxwrap"> <input type="checkbox" id="myCheck" onclick="myFunction()"><h4> Live Corporate</h4> </div> <p id="text" style="display:none"><span>$350.00</span> </p> <div class="checkboxwrap"> <input type="checkbox" id="myCheck" onclick="myFunction()"><h4> Live & Recording Combo - Single Viewer</h4></div> <p id="text" style="display:none">testing</p> <div class="checkboxwrap"> <input type="checkbox" id="myCheck" onclick="myFunction()"><h4>Live & Recording Combo - Corporate Package</h4></div> <div class="checkboxwrap"> <input type="checkbox" id="myCheck" onclick="myFunction()"> <h4>Recording - Single</h4></div> <div class="checkboxwrap"> <input type="checkbox" id="myCheck" onclick="myFunction()">Recording -- Corporate</h4></div> <li class="grey"><a href="#" class="button">Add to Cart</a></li> </ul> </div> <script> function myFunction() { var checkBox = document.getElementById("myCheck"); var text = document.getElementById("text"); if (checkBox.checked == true) { text.style.display = "block"; } else { text.style.display = "none"; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/#findComment-1560365 Share on other sites More sharing options...
Barand Posted August 16, 2018 Share Posted August 16, 2018 IDs must be unique. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/#findComment-1560366 Share on other sites More sharing options...
cyberRobot Posted August 16, 2018 Share Posted August 16, 2018 Note that you can pass the checkbox element to the function using "this". For example: <input type="checkbox" id="myCheck" onclick="myFunction(this)"> The function definition would then look like this <script> function myFunction(checkBox) { var text = document.getElementById("text"); if (checkBox.checked == true) { text.style.display = "block"; } else { text.style.display = "none"; } } </script> Side note: you should use <label> tags to connect your checkboxes with the corresponding labels. For example: <label><input type="checkbox" id="myCheck" onclick="myFunction(this)"> Live Single</label> That way you can click the "Live Single" text to check the box. Also note that I removed the <h4> tags. Those tags should be reserved for actual page headings. Using them for styling purposes causes confusion for people visiting your website with assistive technologies like screen readers. If you need the text labels to appear larger or in bold, you could use CSS on the <label> tags. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/#findComment-1560367 Share on other sites More sharing options...
gizmola Posted August 16, 2018 Share Posted August 16, 2018 12 hours ago, Barand said: IDs must be unique. This is an important point that illustrates that you could improve your code with a better understanding of the html standards. Here's a nice MDN page that covers the 'id' attribute. Keep in mind that the javascript DOM function getElementById() assumes that it will return one and only one object. If you want to address via style or selection a number of items on a page, you should either find them via the type of object, or as children of their container, or with a shared style class. Quote Link to comment https://forums.phpfreaks.com/topic/307618-hello-everyone-i-dont-know-much-about-javascript-but-i-managed-to-put-the-following-code-together/#findComment-1560368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.