0

I have a list of checkboxes which will repeat themselves from backend... but on frontend I have a list of 4 checkboxes and i want to show a textbox on every checkbox click.

That is if one checkbox is clicked then 1 text box should be shown and if 4 checkboxes are clicked then 4 textboxes should be shown.

I am able to do this if I trigger different event on different text box but that won't work when data is coming anonymously.

This is my html markup:

                        <form>
                            <div class="form-group">
                                <label><strong>Events Hosted</strong><sup class = "venue-imp">*</sup></label>
                                <br />
                                <div class="row">
                                    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
                                        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
                                            <label class="checklist" for="wr">
                        <input type="checkbox" value="" id="wr">Wedding &amp;Reception
                        <span class="checkmark"></span></label>
                                       <div id="yesWR" style="display: none">
                                                <input type="number" id="yesWR" placeholder="Hosted!!" />
                                            </div>
                                        </div>
                                        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
                                            <label class="checklist" for="weddings">
                        <input type="checkbox" value="" id="weddings">Weddings
                        <span class="checkmark"></span></label>
                                       <div id="yesWed" style="display: none">
                                                <input type="number" id="yesWed" placeholder="Hosted!!" />
                                            </div>

                                        </div>
                                        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
                                            <label class="checklist" for="bday">
                        <input type="checkbox" value="" id="bday">Bday Party
                        <span class="checkmark"></span></label>
                                            <div id="yesBday" style="display: none">
                                                <input type="number" id="yesBday" placeholder="Hosted!!" />
                                            </div>
                                        </div>
                                        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
                                            <label class="checklist" for="anniversary">
                        <input type="checkbox" value="" id="anniversary">Anniversary
                        <span class="checkmark"></span></label>
                                            <div id="yesAnn" style="display: none">
                                                <input type="number" id="yesAnn" placeholder="Hosted!!" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>

and this is the jQuery code

/*anniversary*/
$(function () {
    $("#anniversary").click(function () {
        if ($(this).is(":checked")) {
            $("#yesAnn").show();
        } else {
            $("#yesAnn").hide();
        }
    });
});
/*bday*/
$(function () {
    $("#bday").click(function () {
        if ($(this).is(":checked")) {
            $("#yesBday").show();
        } else {
            $("#yesBday").hide();
        }
    });
});
/*Weddings*/
$(function () {
    $("#weddings").click(function () {
        if ($(this).is(":checked")) {
            $("#yesWed").show();
        } else {
            $("#yesWed").hide();
        }
    });
});
/*Weddings and Reception*/
$(function () {
    $("#wr").click(function () {
        if ($(this).is(":checked")) {
            $("#yesWR").show();
        } else {
            $("#yesWR").hide();
        }
    });
});

now for every checkbox i am performing different event which is not the solution help!!

2 Answers 2

1

Give class on every check box.Use parent and find div to show and hide.No need to repeat your code.

 $(".checkbox").click(function() {
   if ($(this).is(":checked")) {
     $(this).parent().parent().find('div').show();
     //$("#yesAnn").show();
   } else {
     $(this).parent().parent().find('div').hide();
   }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div class="form-group">
<label><strong>Events Hosted</strong><sup class = "venue-imp">*</sup></label>
<br />
<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
            <label class="checklist" for="wr">
                    <input type="checkbox" class='checkbox' value="" id="wr">Wedding &amp;Reception
                    <span class="checkmark"></span></label>
            <div id="yesWR" style="display: none">
                <input type="number" id="yesWR" placeholder="Hosted!!" />
            </div>
        </div>
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
            <label class="checklist" for="weddings">
                    <input type="checkbox" class='checkbox' value="" id="weddings">Weddings
                    <span class="checkmark"></span></label>
            <div id="yesWed" style="display: none">
                <input type="number" id="yesWed" placeholder="Hosted!!" />
            </div>

        </div>
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
            <label class="checklist" for="bday">
                    <input type="checkbox" class='checkbox' value="" id="bday">Bday Party
                    <span class="checkmark"></span></label>
            <div id="yesBday" style="display: none">
                <input type="number" id="yesBday" placeholder="Hosted!!" />
            </div>
        </div>
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
            <label class="checklist" for="anniversary">
                    <input type="checkbox" class='checkbox' value="" id="anniversary">Anniversary
                    <span class="checkmark"></span></label>
            <div id="yesAnn" style="display: none">
                <input type="number" id="yesAnn" placeholder="Hosted!!" />
            </div>
        </div>
    </div>
</div>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

the problem remains the same i have different data-id for different checkboxes which will definelty show a textbox when clicked but it won't work dynamically as the data-id never change dynamically
Use class instead of data-id like my answer.
where have u used data-id in your jQuery?
could you send the snippet plz
I am not using data-id i am using class instead of data-id.class='checkbox' on every checkbox.See updated answer for 4 check box.
0

Try adding data-id attribute containing the textbox that you need to show in each of your checkbox, so that you can have the textbox anywhere in your form to show, and can handle click with same function to show or hide the textboxes.

/*anniversary*/
$(function() {
  $('input[type="checkbox"]').click(function() {
  var id = $(this).attr("data-id");
    
    if ($(this).is(":checked")) {
      $('#'+id).show();
    } else {
      $('#'+id).hide();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <div class="form-group">
    <label><strong>Events Hosted</strong><sup class="venue-imp">*</sup></label>
    <br />
    <div class="row">
      <div class="col-lg-12 col-md-12 col-xs-12 col-sm-12">
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
          <label class="checklist" for="wr">
            <input type="checkbox" value="" id="wr" data-id="yesWR">Wedding &amp;Reception
            <span class="checkmark"></span></label>
          <div id="yesWR" style="display: none">
            <input type="number" id="yesWR" placeholder="Hosted!!" />
          </div>
        </div>
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
          <label class="checklist" for="weddings">
            <input type="checkbox" value="" id="weddings" data-id="yesWed">Weddings
            <span class="checkmark"></span></label>
          <div id="yesWed" style="display: none">
            <input type="number" id="yesWed" placeholder="Hosted!!" />
          </div>

        </div>
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
          <label class="checklist" for="bday">
            <input type="checkbox" value="" id="bday" data-id="yesBday">Bday Party
            <span class="checkmark"></span></label>
          <div id="yesBday" style="display: none">
            <input type="number" id="yesBday"  placeholder="Hosted!!" />
          </div>
        </div>
        <div class="col-lg-3 col-md-3 col-xs-6 col-sm-6">
          <label class="checklist" for="anniversary">
            <input type="checkbox" value="" id="anniversary" data-id="yesAnn">Anniversary
            <span class="checkmark"></span></label>
          <div id="yesAnn" style="display: none">
            <input type="number" id="yesAnn"  placeholder="Hosted!!" />
          </div>
        </div>
      </div>
    </div>
  </div>
</form>

1 Comment

the problem remains the same i have different data-id for different checkboxes which will definelty show a textbox when clicked but it won't work dynamically as the data-id never change dynamically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.