alanl1 Posted June 11, 2013 Share Posted June 11, 2013 I have a function as shown below that makes dropdown boxes appear and disappear based on a checkbox being ticked or unticked. Is there a way to loop through these as there could be 2 dropdowns or maybe even 50. I have tried the for loop within the function but it does not seem to like it! any ideas??? function show_hide( isChecked ) { alert(isChecked) ; //Can i loop through the dropdowns rather than hardcode them all as the number is unknow per spreadsheet. // This for loop does not work... foreach ($_POST['secondDD'] as $boxIndex=>$boxValue) { alert(document.getElementById('secondDD[$boxIndex]').value) ; } document.getElementById('secondDD[1]').style.display = ( isChecked ? 'block' : 'none' ); document.getElementById('secondDD[1]').disabled = !isChecked; document.getElementById('secondDD[2]').style.display = ( isChecked ? 'block' : 'none' ); document.getElementById('secondDD[2]').disabled = !isChecked; } Quote Link to comment Share on other sites More sharing options...
Barand Posted June 12, 2013 Share Posted June 12, 2013 PHP runs on the server. JS runs on the client once PHP has rendered the page. Quote Link to comment Share on other sites More sharing options...
alanl1 Posted June 12, 2013 Author Share Posted June 12, 2013 yes, so can they be looped through or is it impossible thanks Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted June 12, 2013 Share Posted June 12, 2013 (edited) You might want to try a JQuery approach. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function() { $('input[type=checkbox]').click(function(){ $('input[type=checkbox]').hide(); //alert("That was clicked"); }); }); I only tried it with on checkbox, but this should give you some direction. Edited June 12, 2013 by rwhite35 Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted June 12, 2013 Share Posted June 12, 2013 (edited) You might want to try a JQuery approach. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript"> $(document).ready(function() { $('input[type=checkbox]').click(function(){ $('input[type=checkbox]').hide(); //alert("That was clicked"); }); }); I only tried it with on checkbox, but this should give you some direction. I should add to this and say if you have already ran the PHP code and output the page, then any client side functionality (like hiding checkboxes) should be handled on the client side. If on the next step, you only want to show the checkboxes that were not (or were) selected, then you would have another PHP code block on the next page to display what you want. Does that make sense? Edited June 12, 2013 by rwhite35 Quote Link to comment Share on other sites More sharing options...
alanl1 Posted June 12, 2013 Author Share Posted June 12, 2013 i tried putting that in but it just shows errors Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted June 12, 2013 Share Posted June 12, 2013 i tried putting that in but it just shows errors What was the error? Quote Link to comment Share on other sites More sharing options...
alanl1 Posted June 12, 2013 Author Share Posted June 12, 2013 just says there is a syntax error Quote Link to comment Share on other sites More sharing options...
Solution trq Posted June 12, 2013 Solution Share Posted June 12, 2013 You better figure out what that syntax error is then and come back when your more willing to provide information. Quote Link to comment 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.