I’ve made a form to process user input using PHP, and the first question asks the user to select one of three radio buttons to fill in the “type” parameter – the available options are “book”, “journal”, and “website”, and the code looks like this:
<strong>Type of work:</strong>
<input type="radio" name="type" id="book" value="book" checked="checked" /> <label for="book">Book</label>
<input type="radio" name="type" id="journal" value="journal" /> <label for="journal">Journal</label>
<input type="radio" name="type" id="website" value="website" /> <label for="website">Website</label>
Further down the page, I have three fieldsets (using <fieldset>), each corresponding to one of the types. I’d like to have only one of these display at a time, depending on which radio button is selected, to make the page look cleaner.
Unfortunately, I’m a total JavaScript noob, and my last attempt broke things really badly. The fieldsets already have IDs (boxBook, boxJournal, and boxWebsite), although they currently don’t do anything special.
If it affects anything, I’d like the output to be valid HTML5, and to degrade gracefully, displaying all three fieldsets if the user has JS disabled.
Any help would be greatly appreciated ^^
Using jQuery I’d suggest:
JS Fiddle demo.
Incidentally, rather than performing string manipulation on the
idof theradioinput, it’d be easier to either use adata-*attribute to specify the preciseidof the targeted element:And use:
JS Fiddle demo.
Edited the latter code-block to meet the OP’s requirements:
JS Fiddle demo.
Though, frankly, I think I’ve over-complicated it quite a bit. But it does work, and meets the needs specified in the comment: