Jump to content

looping through dropdown box ids


alanl1
Go to solution Solved by trq,

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
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.

Edited by rwhite35
Link to comment
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.

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 by rwhite35
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.