I'm trying to build an html form with custom styled file inputs. I want the icon of the label to change once the user has selected a file to upload. I came up with some jquery-code but it changes the icon on all of the inputs at once rather then changing it only on the current one.
Does anyone have an idea how to change the icon input by input?
Thanks.
My code:
$("#file-to-upload").change(function(){
$(".file-upload").css("background", "url(https://css-tricks.com/apple-touch-icon.png) 2% / 45px no-repeat #ececec");
});
input[type="file"] {
display: none;
}
.file-upload {
display: block;
width: 260px;
padding: 10px 16px 10px 56px;
border: none;
outline: none;
margin-bottom: 10px;
font: 16px/28px 'Open Sans','Helvetica Neue',Helvetica,sans-serif;
color: #3f3f3f;
font-weight: 300;
background: #ececec;
-webkit-border-radius: 0;
cursor: pointer;
background: url(http://cdn.sstatic.net/stackoverflow/img/favicon.ico?v=4f32ecc8f43d) 2% / 45px no-repeat #ececec;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div><label for="file-to-upload" class="file-upload">File input 1</label>
<input type="file" name="file-to-upload" id="file-to-upload"></div>
<div><label for="file-to-upload" class="file-upload">File input 2</label>
<input type="file" name="file-to-upload" id="file-to-upload"></div>