0

Im trying to add additional fields in case when my user has filled up one group of inputs. So the idea is that when he finishes filling up the last input in that group, another group of inputs for same kind of items collapses/appears under existing one. One group of items consists of three inputs. After filling up that one, another group of inputs would appear under. I will present my code under. Hope someone can help me!

<div class="new-providers">
 <div class="provider-append" id="toggleExtraProvider">
 <div class="form-group">
 <label>Provider</label>
 <input type="text" class="form-control" id="practiceProvider" placeholder="Full provider name with coredentials" />
 </div>
 <div class="form-group">
 <label>NPI</label>
 <input type="text" class="form-control" id="providerNPI" placeholder=" NPI" />
 </div>
 <div class="form-group">
 <label>MM #</label>
 <input type="text" class="form-control" id="providerM" placeholder="M Provider #" />
 </div>
 <hr />
 </div>
</div> 

I tried appending the provider-append class on to the new-providers class

This is my jQuery script:

<script type="text/javascript">
 $(document).ready(function() {
 $("#toggleExtraProvider div.form-group input").each(function() {
 if($(this).val() =! "") {
 var target = $("new-providers");
 target.append(".provider-append");
 }
 }); 
 });​
</script>
aynber
23.3k9 gold badges57 silver badges69 bronze badges
asked Dec 12, 2016 at 17:22
1
  • Do you want to copy the whole div with class "provider-append" ? Commented Dec 12, 2016 at 17:40

1 Answer 1

3

You need to check for empty input box in a particular div use filter() for that and use jQuery clone() to clone the parent div (input group) if all input box filled. And you should use change event instead of input, Since input will be overkill here it will run each time when you change a single character in text box on the other hand change event fires when user finish typing and input box loose focus.

$(document).ready(function () {
 $(document).on("change",".provider-append input",function(){
 var allHaveText;
 var parentDiv = $(this).closest("div.provider-append");
 emptyInputs=parentDiv.find("input[type=text]").filter(function () {
 return !this.value;
 }); 
 if(emptyInputs.length==0)
 {
 newGroup = parentDiv.clone().appendTo(".new-providers"); 
 newGroup.find("input[type=text]").each(function(){
 $(this).val("");
 });
 }
 });
});

Here is a working sample

$(document).ready(function () {
 $(document).on("change",".provider-append input",function(){
 var allHaveText;
 var parentDiv = $(this).closest("div.provider-append");
 emptyInputs=parentDiv.find("input[type=text]").filter(function () {
 return !this.value;
 }); 
 if(emptyInputs.length==0)
 {
 newGroup = parentDiv.clone().appendTo(".new-providers"); 
 newGroup.find("input[type=text]").each(function(){
 $(this).val("");
 });
 }
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="new-providers">
 <div class="provider-append">
 <div class="form-group">
 <label>Provider</label>
 <input type="text" class="form-control" placeholder="Full provider name with coredentials" />
 </div>
 <div class="form-group">
 <label>NPI</label>
 <input type="text" class="form-control" placeholder=" NPI" />
 </div>
 <div class="form-group">
 <label>MM #</label>
 <input type="text" class="form-control" placeholder="M Provider #" />
 </div>
 <hr />
 </div>
</div>

I hope it will help you.

answered Dec 12, 2016 at 18:31
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man!! Never had a question answered in this noble manner.
You are welcome and thanks for the complement bro :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.