Reputation: 731
I'm dynamically appending an input
tag inside a div
tag when i click on a button, so it may happen that the same tag is duplicated in that div. What i want to do is to replace the existing one by the new one. My code is as follows:
<div id="data" name="data"></div>
<input type=button value="+ Ajouter Element" onclick="ajouter();">
<script>
function ajouter() {
for(k=0;k<i;k++) {
h=$("#hauteur"+(k+1)).val();
l=$("#largeur"+(k+1)).val();
p=$("#position"+(k+1)).val();
ch+='<div style="width: '+l+'%; height: '+h+'%; border-style:solid; float:'+p+' "></div>';
$("#data").append('<input type="hidden" name="div'+(k+1)+'" id="div'+(k+1)+'" value="'+(k+1)+';'+h+';'+l+';'+p+'">');}
}
</script>
Upvotes: 0
Views: 148
Reputation: 731
Well i found it by myself :
if($("#div"+(k+1)).length == 0) {
$("#data").append('<input type="hidden" name="div'+(k+1)+'" id="div'+(k+1)+'" value="'+(k+1)+';'+h+';'+l+';'+p+'">');
} else {
$("#data").find("#div"+(k+1)).replaceWith('<input type="hidden" name="div'+(k+1)+'" id="div'+(k+1)+'" value="'+(k+1)+';'+h+';'+l+';'+p+'">');
}
Hope this helps anyone in need for it :)
Upvotes: 1