Jump to content

looping through dropdown box ids


alanl1

Recommended Posts

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;
 
}
 
Link to comment
https://forums.phpfreaks.com/topic/279057-looping-through-dropdown-box-ids/
Share on other sites

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.

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.