user3232801 user3232801 - 1 year ago 63
PHP Question

PHP Form: Show input If is Selected

Put this account out of its misery mods

Just let it die mods

Shout out to jeroen, Fred -ii-, andrewsi, adneal, Vikas Gupta
Y'all the real MVP's

Answer Source

Quite straightforward, adapt as needed to your own context (if you have to get options from somewhere else then you'll need some AJAX) :

http://jsfiddle.net/7aydzs8j/

$(function() {
    $("select").on("change", function() {
        if($(this).val() === "") {
            $("[data-parent]").hide();
        } else {
            $("div[data-parent='" + $(this).val() + "']").show().siblings("[data-parent]").hide();
        }
    });
});
[data-parent] {
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
    <option value="">Choose</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
</select>
<div data-parent="A">
    <input type="text" placeholder="additional info for A">
    <input type="text" placeholder="additional info for A">
    <input type="text" placeholder="additional info for A">
</div>
<div data-parent="B">
    <input type="text" placeholder="additional info for B">
    <input type="text" placeholder="additional info for B">
    <input type="text" placeholder="additional info for B">
</div>
<div data-parent="C">
    <input type="text" placeholder="additional info for C">
    <input type="text" placeholder="additional info for C">
    <input type="text" placeholder="additional info for C">
</div>