admin管理员组文章数量:1396778
I want to clone a whole div with the two objects (select box and input text and his classes), but without the previously entered data and to increment the name and id of the objects.
The name and the id of the standard select is product0 so after cloning the div i want to get product1. The same for the input object. The standard calls amount0 and after cloning the div i want to get amount1.
FYI : In real i am using a PHP function for loading the values inside the dropdown list , the bootstrap-select component for the dropdown object and the jquery mask plugin to show the numberformat.
$(function(){
$( "#addProduct" ).click(function(){
var $products = $("#products").clone();
$('.clonedProduct').last().after($products);
});
});
<script src=".9.1/jquery.min.js"></script>
<div class="row mb-3 clonedProduct" id="products">
<div class="col-lg-3 col-sm-6">
<label for="prod_valid" class="form-label">Product</label>
</div>
<div class="col-lg-3 col-sm-6">
<select class="bg-light border-tr" id="product0" name="product0" aria-label="" title="" data-live-search="true" data-width="100%" required>
<option></option>
<option value="1">Apples</option>
<option value="2">Oranges</option>
<option value="3">Bananas</option>
</select>
</div>
<div class="col-lg-3 col-sm-6">
<label for="amount" class="form-label">Amount</label>
</div>
<div class="col-lg-3 col-sm-6">
<input type="text" class="form-control money" id="amount0" name="amount0" placeholder="0,00 €">
</div>
</div>
<input type="button" class="btn btn-success" value="+" id="addProduct"/>
I want to clone a whole div with the two objects (select box and input text and his classes), but without the previously entered data and to increment the name and id of the objects.
The name and the id of the standard select is product0 so after cloning the div i want to get product1. The same for the input object. The standard calls amount0 and after cloning the div i want to get amount1.
FYI : In real i am using a PHP function for loading the values inside the dropdown list , the bootstrap-select component for the dropdown object and the jquery mask plugin to show the numberformat.
$(function(){
$( "#addProduct" ).click(function(){
var $products = $("#products").clone();
$('.clonedProduct').last().after($products);
});
});
<script src="https://cdnjs.cloudflare/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="row mb-3 clonedProduct" id="products">
<div class="col-lg-3 col-sm-6">
<label for="prod_valid" class="form-label">Product</label>
</div>
<div class="col-lg-3 col-sm-6">
<select class="bg-light border-tr" id="product0" name="product0" aria-label="" title="" data-live-search="true" data-width="100%" required>
<option></option>
<option value="1">Apples</option>
<option value="2">Oranges</option>
<option value="3">Bananas</option>
</select>
</div>
<div class="col-lg-3 col-sm-6">
<label for="amount" class="form-label">Amount</label>
</div>
<div class="col-lg-3 col-sm-6">
<input type="text" class="form-control money" id="amount0" name="amount0" placeholder="0,00 €">
</div>
</div>
<input type="button" class="btn btn-success" value="+" id="addProduct"/>
Share
Improve this question
asked Mar 26 at 12:33
achillixachillix
4795 silver badges17 bronze badges
4
|
1 Answer
Reset to default 2You can also use Range: createContextualFragment() method
const ihm_products = (()=> // IIFE
{
const products = document.querySelector('#products');
const workinRange = new Range();
let prod_id = -1;
add(); // add first...
function add()
{
fragHTML = workinRange.createContextualFragment(`
<div class="product">
<div class="row">
<div class="col-lg-3 col-sm-6">
<label for="prod_valid" class="form-label">Product</label>
</div>
<div class="col-lg-3 col-sm-6">
<select class="bg-light border-tr" id="product${++prod_id}" name="product${prod_id}" aria-label="" title="" data-live-search="true" data-width="100%" required>
<option value="" selected disabled >pick one...</option>
<option value="1">Apples</option>
<option value="2">Oranges</option>
<option value="3">Bananas</option>
</select>
</div>
</div>
<div class="row">
<div class="col-lg-3 col-sm-6">
<label for="amount" class="form-label">Amount</label>
</div>
<div class="col-lg-3 col-sm-6">
<input type="text" class="form-control money" id="amount${prod_id}" name="amount${prod_id}" placeholder="0,00 €">
</div>
</div>
</div>`);
products.append(fragHTML);
}
return { add }
}
)();
document.querySelector('#addProduct')
.addEventListener('click', ihm_products.add );
#products {
padding : 1em;
width : 100%;
}
.product {
margin : 1em;
padding : 1em;
border : solid 1px #ccc;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" />
<div class="row mb-3 clonedProduct" id="products">
</div>
<input type="button" class="btn btn-success" value="+" id="addProduct"/>
本文标签: htmlClone div with Select and input objects without values and increment form namesStack Overflow
版权声明:本文标题:html - Clone div with Select and input objects without values and increment form names - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744144877a2592789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<template>
? – Mister Jojo Commented Mar 26 at 12:37#products
if you are going to clone one product only. Secondly.. as pointed out, you are using a convoluted strategy. It could be done also by having a reference element to be cloned but it makes it odd the way you are choosing how to select any previous "product" to be cloned. While if you just have a template to use as a blueprint you would just need to code the function returning a new iteration of product having the id set based on your criteria – Diego D Commented Mar 26 at 12:43