How does one make a DIV visible if a form action is taken, such as a Radio button selected?
But here is the thing: this DIV that is to become visible and invisible upon radio clicked does not contain a simple short Text message, but instead this DIV contains many Images for selection. I mean I know how to turn on/off a DIV via Javascript command:
Put the div with content in as if it is to be displayed.
Give it an ID, e.g. ShowOnSelection, so it would look like:
<div id="ShowOnSelection"> ... </div>
In your CSS:
#ShowOnSelection{
display: none;
}
In your JavaScript, to trigger it showing or hiding on a button clicking etc:
function TriggerShowOnSelectionElement(){
var possible = "noneblock";
var element = document.getElementByID('ShowOnSelection');
element.style.display = possible.replace(element.style.display, '');
}
<form id="personalInfo">
<input type="radio" name="sex" value="male"> Male
<input type="radio" name="sex" value="female"> Female
</form>
<div id="neeDeclaration">We cannot accept your nee name right now, but rest assured that we're working on it.</div>
Each radio button should have an onclick event attached to it, that shows or hides the div section.
.hidden {
display: none;
}
Ensure that you load the script from just before the </body> tag.
var form = document.getElementById('personalInfo');
var neeDeclaration = document.getElementById('neeDeclaration');
form.elements.sex[0] = function () {
neeDeclaration.className = 'hidden';
};
form.elements.sex[1] = function () {
neeDeclaration.className = '';
};
I have done what you have written below but it is displaying the DIV that
contains the elements that should be hidden until the user clicks on a
form button that indicates they want to see them.
That is I added to the Web page:
<div id = “hiddensection”>
… all bunch of HTML code here…
</div>
and then in the CSS that this web page loaded I added:
#div hiddensection {
display: none;
}
But it is displaying the HTML code that is inside the <div id = “hiddensection”>
Although, I must stress that in terms of technique for the overall solution that you’re using, that directly manipulating element styles is less preferable than adjusting the class name of the element.
@Jake - I have no trouble with him using your solution, as long as it remains clear that non-scripting users won’t be able to use the form. If they are to be catered for as well, there are good solutions that include them too.