Link to home
Start Free TrialLog in
Avatar of gamebits
gamebitsFlag for Canada

asked on

Show Hide span tag with dropdown on checkbox selection

Quite simple question for a javascript expert I'm sure; I want to be able to show an extra input field with his label if the user check a checkbox

<input type="checkbox" name="slabed[]" value="1"> <b>Slabed</b> <span><b>By</b><select name="slabedby[]">
                                                                                                                                     <option value="PCGS">PCGS</option></span>
                                                                                                                                     <option value="ICCS">ICCS</option>
                                                                                                                                            </select>

some details: Dynamic form
                      within a PHP script
                      could have more than one field hence the bracket [] for making array of the form submission.
                      Has to work in all major Browsers
                     
Avatar of Joe Wu
Joe Wu
Flag of Australia image

hi gamebits

what do you mean by an "extra input field"? textbox?
Avatar of gamebits

ASKER

Sorry I didn't mean an input field I meant a dropdown.

Basically

There is a checkbox and when the user check that box a dropdown appear for the user to select what company did the grading and slabed the coin.
ok cool and the dropdown which you are talking about has already been loaded into the page? or do you require an ajax call to build the select-option dropdown dynamically when the user checks the checkbox?
I was thinking of building the option list via a mysql query, just loaded and hidden in the page on page load not thru an AJAX call.

Or it could be just hardcoded on the page.
ok, are you looking for something like this?

Hope i haven't misunderstood your question.

<script type="text/javascript" language="javascript">
function checkField()
{
	if(document.getElementById('cbox').checked == true)
	{
		document.getElementById('selection_box').style.display='';
	}
	else
	{
		document.getElementById('selection_box').style.display='none';
	}
	
}
</script>
 
 
<input type="checkbox" id="cbox" name="slabed[]" value="1" onchange="checkField();"> 
<span id="selection_box" style="display:none;"><b>Slabed</b> <b>By</b>
<select name="slabedby[]">
<option value="PCGS">PCGS</option>
<option value="ICCS">ICCS</option>
</select></span>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@nizsmo

There is a problem with your code, when I check the box I have to click somewhere else on the page to get the hidden span to show, same thing to hide it.

@Zvonko

Your code work but I want to give a chance to nizsmo to fix his code since he was the first to answer the question.

I will award the points accordingly.

 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this was due to ie recognizing the onchange event differently to firefox and safari, but using the onclick everything should work and in all browsers (well, at least ie, firefox and safari)
Thanks both of you either code works great.
Split points is always the better choice ;-)
no problems glad i could help!