vendredi 6 octobre 2017

Show textarea of checked chekbox only and hide when unchecked

I want to show textarea only when checkbox is checked and hide when the checkbox is unchecked. But my current code display all the textarea when certain checkbox is checked. textarea and checkbox are dynamically generated, so there can be any number of checkbox and textarea.

Basically, what I want to do is toggle the textarea on checkbox event.

$( document ).ready(function() {
        $(".with-us").hide();
    $(".with-other").hide();
    $('textarea[name="shopDescription"]').hide();
    $("#work-option :radio").change(function () {
        var workType = $('input[name=work]:checked').val();
        if (workType == "wc") {
                $(".with-other").hide();
            $(".with-us").show();
        } else if (workType == "woc") {
            $(".with-us").hide();
            $(".with-other").show();
        } else {
            $(".with-us").hide();
            $(".with-other").hide();
        }
    });
        $(document).on('click', 'input[name="shop"]', function () {
        if (this.checked) {
                $('textarea[name="shopDescription"]').show();
        } else {
                $('textarea[name="shopDescription"]').hide();
        }
    });
});
<script src="http://ift.tt/Zv5K7g"></script>
<div id="work-option">
        <label>Show your work: </label>
        <div class="options">
            <input type="radio" name="work" value="wc">
            With us
                    <input type="radio" name="work" value="woc">
            With other
        </div>
    </div>
    <div class="with-us">
        <input type="hidden" name="withus" id="withus">
        <input id="work1" type="checkbox" name="shop" value="test1">
        Work 1
        <textarea id="test1Description" placeholder="Description" name="shopDescription"></textarea>

        <input id="work2" type="checkbox" name="shop" value="test2">
        Work 2
        <textarea id="test2Description" placeholder="Description" name="shopDescription"></textarea>

        <input id="work3" type="checkbox" name="shop" value="test3">
        Work 3
        <textarea id="test3Description" placeholder="Description" name="shopDescription"></textarea>

        <input id="work4" type="checkbox" name="shop" value="test4">
        Work 4
        <textarea id="test4Description" placeholder="Description" name="shopDescription"></textarea>

        <input id="work5" type="checkbox" name="shop" value="test5">
        Work 5
        <textarea id="test5Description" placeholder="Description" name="shopDescription"></textarea>

        <input id="work6" type="checkbox" name="shop" value="test6">
        Work 6
        <textarea id="test6Description" placeholder="Description" name="shopDescription"></textarea>
    </div>
    <div class="with-other">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>



Aucun commentaire:

Enregistrer un commentaire