I want to apply a logic from which I can use give new generated class to new added element.
I want that whenever I click on Add Another, it will add new file browse and I can select different images from that. The first image should always be primary and others will be secondary.
When I click on remove link, corresponding file browse and dropdown should be removed.
$(document).ready(function(){
$(".browse").click(function(){
$(".myfile").click();
});
$(".myfile").on('change', function() {
$(".path").val(this.files[0].name);
});
$(".add").click(function(){
$("form").append("<div>"+$("form div:first-child").html()+"<br>"+"</div>");
});
$(".remove").click(function(){
$("form div:last-child").remove();
});
});
.main{
width: 100%;
}
.sub1{
border: 2px solid #808080;
padding-left: 10px;
}
.sub2{
display: flex;
border: 1px solid #808080;
}
.child1{
width: 20%;
padding-left: 10px;
border: 1px solid #808080;
}
.child2{
width: 80%;
padding: 10px;
border: 1px solid #808080;
}
.add{
cursor: pointer;
}
.upload1{
display: none;
}
.browse{
display: inline-block;
width: 80px;
height: 18px;
background-color: #d3d3d3;
color: #000;
border: 1px solid black;
border-radius: 2px;
cursor: pointer;
}
.browsebut{
margin-left: 16px;
}
.myf{
position: absolute;
}
/* position: absolute;
opacity: 5;
top: 74px;
display: -webkit-box; */
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browse</title>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="main">
<div class="sub1">
<p>Property Image</p>
</div>
<div class="sub2">
<div class="child1">
<p>Property Image</p>
</div>
<div class="child2">
<form>
<div class="upload">
<input type="text" name="path" class="path" value="no file selected">
<div id="browse" class="browse"><span class="browsebut">Browse</span></div>
<div class="myf">
<label for="myfile"></label>
<input type="file" class="myfile" name="myfile" accept="image/*" hidden >
</div>
<select id="img">
<option value="1">primary</option>
<option value="1">secondaryimg</option>
</select>
<a href="#" class="remove">Remove</a>
</div>
</form>
<div class="add">
<p>Add Another</p>
</div>
</div>
</div>
</div>[enter image description here][1]
<script src="index.js"></script>
</body>
</html>