What I'm trying to do:
1 - How can I use a second file input to set the background image of a class of divs, in this case the class .logo? (currently, I have one file input, which is setting the background image for the class .background, but I need to add a second file input)
2 - How can I include a link that will "delete" or "trash" the file selected via the file input in order to reset a class of divs as having no background image?
Here is how I've tried to work through this problem so far:
I tried to add a second button, and duplicate the JS and then change the class names, however this broke everything. So I've provided two buttons for the file inputs here. Also, I tried to search for how to delete a file input once uploaded, but I could not understand anything I found. I read through this question about clearing a form, but I could not successfully apply what was described.
Here's what I've tried so far:
$('.verborgen_file').hide();
$('.uploadButton').on('click', function() {
$('.verborgen_file').click();
});
$('.verborgen_file').change(function() {
var file = this.files[0];
var reader = new FileReader();
reader.onloadend = function() {
$('.background').css('background-image', 'url("' + reader.result + '")');
}
if (file) {
reader.readAsDataURL(file);
} else {}
});
.background {
background-image: url("");
background-position: center;
width: 100%;
border: 0px dashed #ddd;
background-color: #fbfbfb;
padding: 10px;
}
.background:hover {
cursor: move;
}
div.bg-img {
background-position: center;
background-repeat: no-repeat;
}
.responsive {
background-image: url("");
background-size: 120%;
background-position: center;
min-height: 20vh;
width: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
position: relative;
}
.medium {
background-image: url("");
background-size: cover;
background-position: center;
height: 150px;
width: 100%;
margin-top: 15px;
margin-bottom: 15px;
position: relative;
}
.logo {
height: 50px;
width: 80px;
background-color: #eee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inputSection">
<h1>Input Section</h1>
Background:
<input type='file' class='verborgen_file' />
<input type="button" value="Upload" class="uploadButton" />
<a href="">delete</a>
<br> Logo:
<input type="button" value="Upload" class="uploadButton" /><a href="">delete</a>
</div>
<br>
<div class="imageSection">
<h1>Divs with Images Section</h1>
<div class="background responsive bg-img">
<div class="logo">
</div>
</div>
<div class="background medium bg-img">
<div class="logo">
</div>
</div>
</div>