show an existing div with drop-down button inside a clone?












1















I have created a clone of a div where i have some drop-down buttons. In my original one the drop-down buttons works perfectly (you will see in the code). But in my clone the drop-down button is shown but when i clicked them i want them to work like the real one. Another thing, i wish my clone will always be displayed after the selected service list. i.e. if i choose pant, then the div of 'pant' will display and after that there will be the 'add' button, the pant div will be remain same and by pressing the 'add' button i can add more fields. i.e. i can display the 'shirt' div by selecting the 'shirt' from the clone 'select service'. I seek the result in jquery. TIA






$(function() {
$('#selectService').change(function() {
$('.hidden-fields').hide();
var $target = $(this).val();
$('#' + $target).show();
});

//creating a clone
// but how i add the same functionality????
$("#add").click(function() {
var $orderClone = $("#orderClone:last");
var $clone = $orderClone.clone();
$clone.find('*').val('');
$orderClone.after($clone);
});
});

.hidden-fields {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orderClone" class="row">
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Service</th>
</tr>
<tr>
<td>
<select id="selectService" name="service" class="form-controlservice">
<option disabled="" selected="">Select Service</option>
<option id="one" value="shirt">Shirt</option>
<option value="pant">Pant</option>
<option value="suit">Suit</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<div class="col-md-4 hidden-fields" id="shirt">
<h4><b>Shirt</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Body</label>
<input type="text" class="form-control" name="body" />
</div>
<div class="form-group required">
<label class="control-label">Shoulder</label>
<input type="text" class="form-control" name="shoulder" />
</div>
<div class="form-group required">
<label class="control-label">Neck</label>
<input type="text" class="form-control" name="neck" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="pant">
<h4><b>Pant</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Length</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Thigh</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="suit">
<h4><b>Suit</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Name</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<br>
<button type="button" id="add" name="add" class="btn btn-success" >Add</button>












share|improve this question


















  • 1





    var $orderClone = $("#orderClone:last"); IDs should be unique in a single document. You should use a class instead.

    – CertainPerformance
    Nov 15 '18 at 22:16











  • Additionally, the .click event is only going to bind to elements that exist at the time the script is run. You should consider event delegation for your event binding. While it's "sloppy" to bind to the document, I don't see any other parent elements to use in this example: $(document).on('click', '#add') <-- which, per previous comments, should be a class, not an ID - jQuery will NOT properly work when multiple elements with the same ID exist.

    – cale_b
    Nov 15 '18 at 22:20
















1















I have created a clone of a div where i have some drop-down buttons. In my original one the drop-down buttons works perfectly (you will see in the code). But in my clone the drop-down button is shown but when i clicked them i want them to work like the real one. Another thing, i wish my clone will always be displayed after the selected service list. i.e. if i choose pant, then the div of 'pant' will display and after that there will be the 'add' button, the pant div will be remain same and by pressing the 'add' button i can add more fields. i.e. i can display the 'shirt' div by selecting the 'shirt' from the clone 'select service'. I seek the result in jquery. TIA






$(function() {
$('#selectService').change(function() {
$('.hidden-fields').hide();
var $target = $(this).val();
$('#' + $target).show();
});

//creating a clone
// but how i add the same functionality????
$("#add").click(function() {
var $orderClone = $("#orderClone:last");
var $clone = $orderClone.clone();
$clone.find('*').val('');
$orderClone.after($clone);
});
});

.hidden-fields {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orderClone" class="row">
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Service</th>
</tr>
<tr>
<td>
<select id="selectService" name="service" class="form-controlservice">
<option disabled="" selected="">Select Service</option>
<option id="one" value="shirt">Shirt</option>
<option value="pant">Pant</option>
<option value="suit">Suit</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<div class="col-md-4 hidden-fields" id="shirt">
<h4><b>Shirt</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Body</label>
<input type="text" class="form-control" name="body" />
</div>
<div class="form-group required">
<label class="control-label">Shoulder</label>
<input type="text" class="form-control" name="shoulder" />
</div>
<div class="form-group required">
<label class="control-label">Neck</label>
<input type="text" class="form-control" name="neck" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="pant">
<h4><b>Pant</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Length</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Thigh</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="suit">
<h4><b>Suit</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Name</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<br>
<button type="button" id="add" name="add" class="btn btn-success" >Add</button>












share|improve this question


















  • 1





    var $orderClone = $("#orderClone:last"); IDs should be unique in a single document. You should use a class instead.

    – CertainPerformance
    Nov 15 '18 at 22:16











  • Additionally, the .click event is only going to bind to elements that exist at the time the script is run. You should consider event delegation for your event binding. While it's "sloppy" to bind to the document, I don't see any other parent elements to use in this example: $(document).on('click', '#add') <-- which, per previous comments, should be a class, not an ID - jQuery will NOT properly work when multiple elements with the same ID exist.

    – cale_b
    Nov 15 '18 at 22:20














1












1








1








I have created a clone of a div where i have some drop-down buttons. In my original one the drop-down buttons works perfectly (you will see in the code). But in my clone the drop-down button is shown but when i clicked them i want them to work like the real one. Another thing, i wish my clone will always be displayed after the selected service list. i.e. if i choose pant, then the div of 'pant' will display and after that there will be the 'add' button, the pant div will be remain same and by pressing the 'add' button i can add more fields. i.e. i can display the 'shirt' div by selecting the 'shirt' from the clone 'select service'. I seek the result in jquery. TIA






$(function() {
$('#selectService').change(function() {
$('.hidden-fields').hide();
var $target = $(this).val();
$('#' + $target).show();
});

//creating a clone
// but how i add the same functionality????
$("#add").click(function() {
var $orderClone = $("#orderClone:last");
var $clone = $orderClone.clone();
$clone.find('*').val('');
$orderClone.after($clone);
});
});

.hidden-fields {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orderClone" class="row">
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Service</th>
</tr>
<tr>
<td>
<select id="selectService" name="service" class="form-controlservice">
<option disabled="" selected="">Select Service</option>
<option id="one" value="shirt">Shirt</option>
<option value="pant">Pant</option>
<option value="suit">Suit</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<div class="col-md-4 hidden-fields" id="shirt">
<h4><b>Shirt</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Body</label>
<input type="text" class="form-control" name="body" />
</div>
<div class="form-group required">
<label class="control-label">Shoulder</label>
<input type="text" class="form-control" name="shoulder" />
</div>
<div class="form-group required">
<label class="control-label">Neck</label>
<input type="text" class="form-control" name="neck" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="pant">
<h4><b>Pant</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Length</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Thigh</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="suit">
<h4><b>Suit</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Name</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<br>
<button type="button" id="add" name="add" class="btn btn-success" >Add</button>












share|improve this question














I have created a clone of a div where i have some drop-down buttons. In my original one the drop-down buttons works perfectly (you will see in the code). But in my clone the drop-down button is shown but when i clicked them i want them to work like the real one. Another thing, i wish my clone will always be displayed after the selected service list. i.e. if i choose pant, then the div of 'pant' will display and after that there will be the 'add' button, the pant div will be remain same and by pressing the 'add' button i can add more fields. i.e. i can display the 'shirt' div by selecting the 'shirt' from the clone 'select service'. I seek the result in jquery. TIA






$(function() {
$('#selectService').change(function() {
$('.hidden-fields').hide();
var $target = $(this).val();
$('#' + $target).show();
});

//creating a clone
// but how i add the same functionality????
$("#add").click(function() {
var $orderClone = $("#orderClone:last");
var $clone = $orderClone.clone();
$clone.find('*').val('');
$orderClone.after($clone);
});
});

.hidden-fields {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orderClone" class="row">
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Service</th>
</tr>
<tr>
<td>
<select id="selectService" name="service" class="form-controlservice">
<option disabled="" selected="">Select Service</option>
<option id="one" value="shirt">Shirt</option>
<option value="pant">Pant</option>
<option value="suit">Suit</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<div class="col-md-4 hidden-fields" id="shirt">
<h4><b>Shirt</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Body</label>
<input type="text" class="form-control" name="body" />
</div>
<div class="form-group required">
<label class="control-label">Shoulder</label>
<input type="text" class="form-control" name="shoulder" />
</div>
<div class="form-group required">
<label class="control-label">Neck</label>
<input type="text" class="form-control" name="neck" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="pant">
<h4><b>Pant</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Length</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Thigh</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="suit">
<h4><b>Suit</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Name</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<br>
<button type="button" id="add" name="add" class="btn btn-success" >Add</button>








$(function() {
$('#selectService').change(function() {
$('.hidden-fields').hide();
var $target = $(this).val();
$('#' + $target).show();
});

//creating a clone
// but how i add the same functionality????
$("#add").click(function() {
var $orderClone = $("#orderClone:last");
var $clone = $orderClone.clone();
$clone.find('*').val('');
$orderClone.after($clone);
});
});

.hidden-fields {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orderClone" class="row">
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Service</th>
</tr>
<tr>
<td>
<select id="selectService" name="service" class="form-controlservice">
<option disabled="" selected="">Select Service</option>
<option id="one" value="shirt">Shirt</option>
<option value="pant">Pant</option>
<option value="suit">Suit</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<div class="col-md-4 hidden-fields" id="shirt">
<h4><b>Shirt</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Body</label>
<input type="text" class="form-control" name="body" />
</div>
<div class="form-group required">
<label class="control-label">Shoulder</label>
<input type="text" class="form-control" name="shoulder" />
</div>
<div class="form-group required">
<label class="control-label">Neck</label>
<input type="text" class="form-control" name="neck" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="pant">
<h4><b>Pant</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Length</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Thigh</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="suit">
<h4><b>Suit</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Name</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<br>
<button type="button" id="add" name="add" class="btn btn-success" >Add</button>





$(function() {
$('#selectService').change(function() {
$('.hidden-fields').hide();
var $target = $(this).val();
$('#' + $target).show();
});

//creating a clone
// but how i add the same functionality????
$("#add").click(function() {
var $orderClone = $("#orderClone:last");
var $clone = $orderClone.clone();
$clone.find('*').val('');
$orderClone.after($clone);
});
});

.hidden-fields {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orderClone" class="row">
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Service</th>
</tr>
<tr>
<td>
<select id="selectService" name="service" class="form-controlservice">
<option disabled="" selected="">Select Service</option>
<option id="one" value="shirt">Shirt</option>
<option value="pant">Pant</option>
<option value="suit">Suit</option>
</select>
</td>
</tr>
</table>
</div>
</div>
<div class="col-md-4 hidden-fields" id="shirt">
<h4><b>Shirt</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Body</label>
<input type="text" class="form-control" name="body" />
</div>
<div class="form-group required">
<label class="control-label">Shoulder</label>
<input type="text" class="form-control" name="shoulder" />
</div>
<div class="form-group required">
<label class="control-label">Neck</label>
<input type="text" class="form-control" name="neck" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="pant">
<h4><b>Pant</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Length</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Thigh</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<div class="col-md-4 hidden-fields" id="suit">
<h4><b>Suit</b></h4>
<hr/>
<div class="form-group required"> <label class="control-label">Name</label>
<input type="text" class="form-control" name="length" />
</div>
<div class="form-group required">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="thigh" />
</div>
</div>
<br>
<button type="button" id="add" name="add" class="btn btn-success" >Add</button>






jquery html css






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 22:15









Mkh ShimulMkh Shimul

222




222








  • 1





    var $orderClone = $("#orderClone:last"); IDs should be unique in a single document. You should use a class instead.

    – CertainPerformance
    Nov 15 '18 at 22:16











  • Additionally, the .click event is only going to bind to elements that exist at the time the script is run. You should consider event delegation for your event binding. While it's "sloppy" to bind to the document, I don't see any other parent elements to use in this example: $(document).on('click', '#add') <-- which, per previous comments, should be a class, not an ID - jQuery will NOT properly work when multiple elements with the same ID exist.

    – cale_b
    Nov 15 '18 at 22:20














  • 1





    var $orderClone = $("#orderClone:last"); IDs should be unique in a single document. You should use a class instead.

    – CertainPerformance
    Nov 15 '18 at 22:16











  • Additionally, the .click event is only going to bind to elements that exist at the time the script is run. You should consider event delegation for your event binding. While it's "sloppy" to bind to the document, I don't see any other parent elements to use in this example: $(document).on('click', '#add') <-- which, per previous comments, should be a class, not an ID - jQuery will NOT properly work when multiple elements with the same ID exist.

    – cale_b
    Nov 15 '18 at 22:20








1




1





var $orderClone = $("#orderClone:last"); IDs should be unique in a single document. You should use a class instead.

– CertainPerformance
Nov 15 '18 at 22:16





var $orderClone = $("#orderClone:last"); IDs should be unique in a single document. You should use a class instead.

– CertainPerformance
Nov 15 '18 at 22:16













Additionally, the .click event is only going to bind to elements that exist at the time the script is run. You should consider event delegation for your event binding. While it's "sloppy" to bind to the document, I don't see any other parent elements to use in this example: $(document).on('click', '#add') <-- which, per previous comments, should be a class, not an ID - jQuery will NOT properly work when multiple elements with the same ID exist.

– cale_b
Nov 15 '18 at 22:20





Additionally, the .click event is only going to bind to elements that exist at the time the script is run. You should consider event delegation for your event binding. While it's "sloppy" to bind to the document, I don't see any other parent elements to use in this example: $(document).on('click', '#add') <-- which, per previous comments, should be a class, not an ID - jQuery will NOT properly work when multiple elements with the same ID exist.

– cale_b
Nov 15 '18 at 22:20












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328653%2fshow-an-existing-div-with-drop-down-button-inside-a-clone%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328653%2fshow-an-existing-div-with-drop-down-button-inside-a-clone%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues