By default different web browser renders the HTML <input type="file">
differently, also if you try to style them with the CSS properties it doesn't work. But, you can use the CSS opacity
and position
property in combination with the jQuery change()
method to create your own HTML custom file upload form control that is consistent across the browsers.
Let's take a look at the following example to understand how it basically works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Custom File Input Field with CSS and jQuery</title>
<style>
.custom-file-input{
display: inline-block;
overflow: hidden;
position: relative;
}
.custom-file-input input[type="file"]{
width: 100%;
height: 100%;
opacity: 0;
filter: alpha(opacity=0);
zoom: 1; /* Fix for IE7 */
position: absolute;
top: 0;
left: 0;
z-index: 999;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$('.custom-file-input input[type="file"]').change(function(e){
$(this).siblings('input[type="text"]').val(e.target.files[0].name);
});
});
</script>
</head>
<body>
<form>
<div class="custom-file-input">
<input type="file">
<input type="text">
<input type="button" value="Attach">
</div>
</form>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to find mouse position relative to the document using jQuery
Use the jQuery event.pageX and...Laravel 7 Send Mail Using Markdown Example
Today, markdown laravel 7 mail is our ma...How to toggle text inside and element on click using jQuery
Use the jQuery text() method You can...Laravel 8 Realtime Chat Message Application using socket.io, redis, express and nodejs
Laravel provides easy integration with N...Laravel 8 - User Roles and Permissions using Spatie
nowaday our leading subject is laravel 8...