I have 3 radio buttons, they’re called “Easy” “Medium” and “Hard”. Each one is linked to a separate Javascript function. These functions show one div while hiding two other divs.
The purpose of this is for the user to be able to choose an option and see the div for that option, for example, if they press the “Easy” radio button then the “Easy” div should be shown, and the “Medium” and “Hard” divs should be hidden.
I’ve been trying to get this to work for a week now and I think I’ve tried every related suggestion on stackoverflow, but I hope there’s just something small that I’m missing.
These are the radio buttons in my HTML:
<td width="100"><div align="center" class="radio">
<input type="radio" name='thing' value='valuable' id="easy" onclick="showEasy()"/></div>
</td>
<td width="100"><div align="center" class="radio">
<input type="radio" name='thing' value='valuable' id="medium" onclick="showMedium()"/></div>
</td>
<td width="100"><div align="center" class="radio">
<input type="radio" name='thing' value='valuable' id="hard" onclick="showHard()"/></div>
</td>
These are the DIVs I want to show/hide:
<td colspan="8" rowspan="12" align="center">
<div align="center" id="easyDIV"><img src="/images/pic1.jpg" alt="If you can see this, then the image didn't load properly." class="distort1" id="sample"/></div>
<div align="center" id="mediumDIV"><img src="/images/pic2.jpg" alt="If you can see this, then the image didn't load properly." class="distort2" id="sample1"/></div>
<div align="center" id="hardDIV"><img src="/images/pic3.jpg" alt="If you can see this, then the image didn't load properly." class="distort3" id="sample2"/></div>
</td>
And this is the script I have at the top of my page, each function should show one DIV at a time:
<script>
function showEasy()
{
$("#easyDIV").removeClass("displaynone");
//Make sure mediumDIV is not visible
$("#mediumDIV").addClass("displaynone");
//Make sure hardDIV is not visible
$("#hardDIV").addClass("displaynone");
}
function showMedium()
{
$("#mediumDIV").removeClass("displaynone");
//Make sure easyDIV is not visible
$("#easyDIV").addClass("displaynone");
//Make sure hardDIV is not visible
$("#hardDIV").addClass("displaynone");
}
function showHard()
{
$("#hardDIV").removeClass("displaynone");
//Make sure easyDIV is not visible
$("#easyDIV").addClass("displaynone");
//Make sure mediumDIV is not visible
$("#mediumDIV").addClass("displaynone");
}
</script>
I only want the Medium div to be shown at the start, so I have showMedium();
set to run once the page has finished loading, this successfully hides the Easy and Hard divs. The problem is that all 3 divs are visible for a second or two until every page element is finished loading. Once everything’s loaded, showMedium();
runs and the Easy and Hard divs get hidden.
This looks really bad to the user, so I’ve been trying to find a way to have these two divs hidden by default and just have the Medium div showing at the start.
I’ve tried several methods, I’ve tried hiding the two divs by setting their divs to <div hidden>
I’ve also tried hiding the two divs by setting their style to display:none
like this:
<div align="center" id="easyDIV" style="display: none;"><img src="" alt="If you can see this, then the image didn't load properly." id="sample"/></div>
And then to show each div I tried changing this style to display: inline
and display: block
by including code to do that inside the Javascript function above.
These attempts didn’t work, the 2 divs were successfully hidden (because they didn’t appear when the page was loading) but whenever I tried clicking on the Easy or Hard radio buttons, while the Medium div disappeared correctly, it was replaced by an empty space, meaning the Easy and Hard divs were still hidden.
I can’t work out how to get this working, it seems like the only way I can have the radio buttons working successfully and showing/hiding the correct divs, is to have all 3 load/appear on screen for a second or two while the page loads. I’d really appreciate any advice on getting this to work the way I’d like it to. I’ll try any suggestions and thank you in advance for your help!