In my user interface, I have implemented three radio buttons labeled as "Easy," "Medium," and "Hard." Each button is assigned to a distinct Javascript function that manipulates the visibility of corresponding div elements. The main purpose behind this setup is to allow users to select an option and view the related content within the designated div.
Despite my efforts over the past week and thorough exploration of various suggestions on platforms like StackOverflow, I seem to be missing a crucial detail for the functionality to work seamlessly.
The HTML code for the radio buttons is structured as follows:
<td width="100"><div align="center" class="radio">
<input type="radio" name='level' value='easy' id="easy" onclick="showEasy()"/></div>
</td>
<td width="100"><div align="center" class="radio">
<input type="radio" name='level' value='medium' id="medium" onclick="showMedium()"/></div>
</td>
<td width="100"><div align="center" class="radio">
<input type="radio" name='level' value='hard' id="hard" onclick="showHard()"/></div>
</td>
Below are the respective div elements that I intend to show/hide dynamically:
<td colspan="8" rowspan="12" align="center">
<div align="center" id="easyDIV"><img src="/images/pic1.jpg" alt="Image not loaded correctly." class="distort1" id="sample"/></div>
<div align="center" id="mediumDIV"><img src="/images/pic2.jpg" alt="Image not loaded correctly." class="distort2" id="sample1"/></div>
<div align="center" id="hardDIV"><img src="/images/pic3.jpg" alt="Image not loaded correctly." class="distort3" id="sample2"/></div>
</td>
Furthermore, I have included Javascript functions at the top of my page to dictate the visibility of each div element based on the selected radio button:
<script>
function showEasy()
{
$("#easyDIV").removeClass("displaynone");
// Ensure mediumDIV is hidden
$("#mediumDIV").addClass("displaynone");
// Ensure hardDIV is hidden
$("#hardDIV").addClass("displaynone");
}
function showMedium()
{
$("#mediumDIV").removeClass("displaynone");
// Ensure easyDIV is hidden
$("#easyDIV").addClass("displaynone");
// Ensure hardDIV is hidden
$("#hardDIV").addClass("displaynone");
}
function showHard()
{
$("#hardDIV").removeClass("displaynone");
// Ensure easyDIV is hidden
$("#easyDIV").addClass("displaynone");
// Ensure mediumDIV is hidden
$("#mediumDIV").addClass("displaynone");
}
</script>
Although my initial intention was to display only the Medium div upon page load by triggering showMedium();
, there seems to be a brief moment where all divs are visible before getting hidden. This creates a suboptimal user experience that I am keen on resolving.
I experimented with different techniques such as using the <div hidden>
attribute or employing display:none
in the style attribute of the divs, along with dynamic changes via Javascript. However, these approaches resulted in unexpected behavior where clicking on Easy or Hard radio buttons left empty spaces instead of displaying the intended div.
If anyone has insights or suggestions on achieving the desired functionality without the temporary display of all divs, I would greatly appreciate your guidance and assistance in optimizing this feature for a smoother user interaction. Thank you in advance for your help!