Created
January 6, 2017 10:49
-
-
Save robertkruis/32683f243bbabc0decb3feefc9ef8d70 to your computer and use it in GitHub Desktop.
Hide and show fields based on radio button value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>How would you like to share your file?</p> | |
<div> | |
<div> | |
<input type="radio" name="resume" id="upload" value="upload"> | |
<label for="upload">Upload file</label> | |
<div id="reveal-upload"> | |
<input type="file" accept="file_extension"> | |
</div> | |
</div> | |
<div> | |
<input type="radio" name="resume" id="link" value="link"> | |
<label for="link">URL</label> | |
<div id="reveal-link"> | |
<input type="url" placeholder="Please submit your link here"> | |
</div> | |
</div> | |
<div> | |
<input type="radio" name="resume" id="email" value="email"> | |
<label for="email">Email</label> | |
<div id="reveal-email"> | |
Please send your file to [email protected] | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var link = $("#reveal-link"); | |
var upload = $("#reveal-upload"); | |
var email = $("#reveal-email"); | |
link.hide(); | |
upload.hide(); | |
email.hide(); | |
var previousChecked; | |
// Select the inputs with the name "resume" | |
$("input[name='resume']").change(function() { | |
$elem = $(this); | |
// Hide the previous selected option | |
if (previousChecked) { | |
previousChecked.hide(); | |
} | |
switch ($elem.val()) { | |
case 'upload': | |
upload.show(); | |
previousChecked = upload; | |
break; | |
case 'link': | |
link.show(); | |
previousChecked = link; | |
break; | |
case 'email': | |
email.show(); | |
previousChecked = email; | |
break; | |
default: | |
previousChecked = undefined; | |
email.hide(); | |
link.hide(); | |
upload.hide(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment