Hello, I'm using php code to retrieving an array from database and place the result into checkboxes but after the last checkbox input there is text input (hidden by default) and the last check box name is 'other'. I want to make the text in visible if 'other' is checked i used this code:

<script type="text/javascript">
			if (document.getElementById('other').checked = true) { 
			document.getElementById('whatever').style.visibility = "visible"; 
			} else { 
			document.getElementById('whatever').style.visibility = "hidden"; 
			} 
			
</script> 
<input name="pVital[]" type="checkbox" id="pVital[]" value="I &amp; O" checked="<?php if (in_array('I &amp; O', $pvitals)) echo "checked"; else echo "unchecked"; ?>" />I &amp; O<br/>
   	  <input <?php if (in_array('Daily Weight', $pvitals)) echo "checked"; else echo "unchecked";  ?> name="pVital[]" type="checkbox" id="pVital[]" value="Daily Weight" />Daily Weight<br/>
   	  <input <?php if (in_array('Foley Catheter', $pvitals)) echo "checked"; else echo "unchecked"; ?> name="pVital[]" type="checkbox" id="pVital[]" value="Foley Catheter" />Foley Catheter<br/>
   	  <input <?php if (in_array('other', $pvitals)) echo "checked"; else echo "unchecked"; ?> name="pVital[]" type="checkbox" id="other"  value="other" />Other<br/>
        <input name="pVital[]" type="text" id="whatever" style="visibility: hidden;" value="<?php if (in_array('other', $pvitals)) echo end($pvitals); ?>">

but this is not work :(
Just i want to make this text input visible whenever 'other' is checked and not necessary wither event is occurred or not.

Recommended Answers

All 3 Replies

Try this code

<script type="text/javascript">
			if (document.getElementById('other').checked = true) { 
			document.getElementById('whatever').style.visibility = "visible"; 
			} else { 
			document.getElementById('whatever').style.visibility = "hidden"; 
			} 
			
</script> 
<input name="pVital[]" type="checkbox" id="pVital[]" value="I &amp; O" checked="<?php if (in_array('I &amp; O', $pvitals)) echo "checked"; else echo "unchecked"; ?>" />I &amp; O<br/>
   	  <input <?php if (in_array('Daily Weight', $pvitals)) echo "checked"; else echo "unchecked";  ?> name="pVital[]" type="checkbox" id="pVital[]" value="Daily Weight" />Daily Weight<br/>
   	  <input <?php if (in_array('Foley Catheter', $pvitals)) echo "checked"; else echo "unchecked"; ?> name="pVital[]" type="checkbox" id="pVital[]" value="Foley Catheter" />Foley Catheter<br/>
   	  <input <?php if (in_array('other', $pvitals)) echo "checked"; else echo "unchecked"; ?> name="pVital[]" type="checkbox" id="other"  value="other" />Other<br/>
        <input name="pVital[]" type="text" id="whatever"   <?php if (in_array('other', $pvitals)) { ?>  style="visibility: visible;"   <?php } else { ?>    style="visibility:hidden" <?php } ?>  value="<?php if (in_array('other', $pvitals)) echo end($pvitals); ?>">

Thank you divyakrishnan, this is work fine after little change in last line because it contain "duplicate attribute" error I change it to: <input name="pVital[]" type="text" id="whatever" <?php if (in_array('other', $pvitals)) { $vis = 'style="visibility: visible;"'; echo $vis; ?> <?php } else { ?> style="visibility:hidden" <?php } ?> value="<?php if (in_array('other', $pvitals)) echo end($pvitals); ?>"> and it works. Thank you again :)

Please mark your thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.