Django Loop for Window expand/collapse without JQuery - html

I'm working using Django. I'm new to coding and working on my first project. I do not know JQuery, but I read that it's possible to collapse/expand a window without JQuery.
I followed this webpage: CSS Expand/Collapse Section (A PEN BY Peter Nguyen)
The problem is that I'm trying to use this in a for loop. I'm able to get the layout how I want it, but when I use expand/collapse it only works for the first item in the loop. So, expand/collapse works perfectly on the first iteration of the loop, but not the rest.
HTML
{% for assignment in assignments %}
<form id=form action= "/project/assignments" method='post'>
{% csrf_token %}
<label class=labels><a href="{% url 'project:assg' assignment_id=assignment.id %}">
Assignment: {{assignment.denominator}} - {{assignment.description}} </a></label>
<input id="toggle" type="checkbox">
<label for="toggle" id=labels2>Criteria</label>
<div id=expand>
<section>
<p>[Number] [Name]: Expand content goes here.
</p>
</section>
</div>
<section>
<textarea id=boxes name="review">{{assignments.review}}</textarea>
<input type="hidden" name="Function_id" value={{assignments.id}}>
<input class=save_tasks type="submit" value="Save">
</section>
</form>
CSS
#toggle{
display: none;
visibility:hidden;
}
#labels2{
display: block;
padding: 0.5em;
text-align: center;
background-color: white;
border-top: 4px solid #5e6b39;
border-left: 4px solid #5e6b39;
border-right: 4px solid #5e6b39;
width: 881px;
color: black;
font-size: 20px;
font-weight: bold;
margin: -6px;
margin-top: 5px;
margin-left: 0px;
}
#labels2:hover {
color: #000;
}
#labels2::before {
font-family: Consolas, monaco, monospace;
font-weight: bold;
font-size: 15px;
content: "+";
vertical-align: text-top;
display: inline-block;
width: 20px;
height: 20px;
margin-right: 3px;
background: radial-gradient(ellipse at center, #CCC 50%, transparent
50%);
}
#expand {
height: 0px;
overflow: hidden;
transition: height 0.5s;
background-color: white;
color:black;
font-size: 20px;
border-bottom: 4px solid #5e6b39;
border-left: 4px solid #5e6b39;
border-right: 4px solid #5e6b39;
width: 881px;
padding: 10px;
/* margin-top: -30px; */
}
#toggle:checked ~ #expand {
height: 180px;
}
#toggle:checked ~ #labels2::before {
content: "-";
}
How do I get the collapse/expand division to expand/collapse for all the items in the loop and not just the first one? Is my Django loop incorrect?
Thanks!

The loop is not the problem here.
You only use IDs in your code, and since they are supposed to be unique on a page, the browser uses only the first element that can be found.
The id global attribute defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
I have adjusted your code once in the necessary places:
.toggle {
display: none;
visibility:hidden;
}
.labels2 {
display: block;
padding: 0.5em;
text-align: center;
background-color: white;
border-top: 4px solid #5e6b39;
border-left: 4px solid #5e6b39;
border-right: 4px solid #5e6b39;
width: 881px;
color: black;
font-size: 20px;
font-weight: bold;
margin: -6px;
margin-top: 5px;
margin-left: 0px;
}
.labels2:hover {
color: #000;
}
.labels2::before {
font-family: Consolas, monaco, monospace;
font-weight: bold;
font-size: 15px;
content: "+";
vertical-align: text-top;
display: inline-block;
width: 20px;
height: 20px;
margin-right: 3px;
background: radial-gradient(ellipse at center, #CCC 50%, transparent 50%);
}
.expand {
height: 0px;
overflow: hidden;
transition: height 0.5s;
background-color: white;
color:black;
font-size: 20px;
border-bottom: 4px solid #5e6b39;
border-left: 4px solid #5e6b39;
border-right: 4px solid #5e6b39;
width: 881px;
padding: 10px;
/* margin-top: -30px; */
}
.toggle:checked ~ .expand {
height: 180px;
}
.toggle:checked ~ .labels2::before {
content: "-";
}
{% for assignment in assignments %}
<form id=form action= "/project/assignments" method='post'>
{% csrf_token %}
<label class=labels>Assignment: {{assignment.denominator}} - {{assignment.description}}</label>
<input id="toggle-{{assignments.id}}" class="toggle" type="checkbox">
<label for="toggle-{{assignments.id}}" class="labels2">Criteria</label>
<div class="expand">
<section>
<p>[Number] [Name]: Expand content goes here.</p>
</section>
</div>
<section>
<textarea id=boxes name="review">{{assignments.review}}</textarea>
<input type="hidden" name="Function_id" value={{assignments.id}}>
<input class=save_tasks type="submit" value="Save">
</section>
</form>
At this point it is important that the input class="toggle" and the label class="labels2" get a unique distinction in the id and the for attribute, so that the click on the label also checks the correct input.
For this you should work with a dynamic value like toggle-{{assignments.id}} - Which I would also highly recommend in principle for all IDs in a loop.
I hope that could help :)

Related

I can't figure out how to control the CSS height of an ahref

I have an HTML page with embedded CSS styling. It has a form with an input button that says "Submit Payment" using #fjButton and height of 54, which gets obeyed. I also have an that has an id #fjButton2 with a height of 14... both set to !important. The #fjButton2 doesn't get obeyed. Im's sure it's some stupid thing, but i just can't figure it out... the stays at 32px no matter what i do. See it at:
https://codepen.io/johnstonf/full/YzwGqNO
<!DOCTYPE html>
<html>
<head>
<title>Test-Store</title>
<link href="https://fonts.googleapis.com/css?family=Fjalla+One|Inter&display=swap" rel="stylesheet">
<!-- Landing page styling -->
<style type="text/css">
body{
background-color: #f2f4f9;
}
h3, h5{
font-family: 'Fjalla One', sans-serif;
font-weight: 100;
}
h3{
font-size: 36px;
}
h5{
font-size: 24px;
}
p, label{
font-family: 'Inter', sans-serif;
}
hr{
color:#fff;
background-color: #fff;
border-top:1px solid #fff;
margin-top:10px;
margin-bottom: 10px;
}
#container{
width:1200px;
margin:0 auto;
padding-top: 3%;
}
.column-6{
text-align: center;
width: 50%;
float:left;
}
#left-wrapper{
background-color: #72cca7;
box-shadow: 0 1px 3px 0 #dce0e6;
border-radius: 14px;
border: 3px solid black;
margin-right: 14px;
margin-top: 14px;
padding: 13px;
}
#right-wrapper{
padding-top: 13px;
margin-top: 13px;
border-radius: 14px;
border: 3px solid black;
margin-right: 14px;
background-color: #72cca7;
box-shadow: 0 1px 3px 0 #dce0e6;
border-radius: 14px;
border: 3px solid black;
margin-right: 14px;
margin-top: 14px;
padding: 13px;
}
#profile-pic{
width:200px;
display:block;
margin: 0 auto;
border-radius: 50%;
float:left;
}
#left-wrapper-header{
min-width:50%;
float:left;
padding-left:20px;
}
#left-wrapper-header h3{
color:#fff;
}
#right-wrapper h3{
color: #10a195;
}
.Z9999{
margin-left: 1px;
width: 99%;
}
/* ------------------------ FORM FIELDS ------------------------ */
.form-field-12{
width: 100%;
margin-bottom: 6px;
padding-top: 3px;
}
.form-field-6{
width: 50%;
float:left;
margin-bottom: 1px;
}
select{
width: 100%;
height: 25px!important;
box-shadow: 0 1px 3px 0 #dce0e6;
padding: 1px 2px;
border-radius: 4px;
background-color: #fff;
border: 1px solid transparent;
font-size: 20px;
color: #44464a;
}
input{
box-shadow: 0 1px 3px 0 #dce0e6;
height:40px;
border-radius: 4px;
background-color: #fff;
border: 1px solid transparent;
color: #44464a;
width: 95%;
padding: 5px;
font-size: 16px;
}
input::placeholder {
color: #9fa1a6;
}
/* ------------------------ BUTTON STYLING ------------------------ */
#fjButton {
box-shadow: 0 1px 3px 0 #dce0e6;
font-family:'Fjalla One', sans-serif;;
border-radius: 4px;
background-color: #fff;
border: 1px solid transparent;
color: #44464a;
height: 54px !important;
width: 30%;
padding: 5px;
font-size: 16px;
text-decoration:none;
margin: 0px;
}
#fjButton2 {
box-shadow: 0 1px 3px 0 #dce0e6;
font-family:'Fjalla One', sans-serif;;
border-radius: 4px;
background-color: #fff;
border: 1px solid transparent;
color: #44464a;
height: 14px !important;
width: 30%;
padding: 5px;
font-size: 16px;
text-decoration:none;
margin: 0px;
}
.myButton:hover {
opacity: .6;
}
#media screen and (max-width:1200px){
#container{
width:80%;
}
.column-6{
width: 100%;
}
}
#media screen and (max-width:800px){
#container{
width:100%;
}
.Z9999{
margin-left: 20px;
width: 100px;
}
}
</style>
<!-- Success page styling -->
<style type="text/css">
h4{
font-family: 'Fjalla One', sans-serif;
font-weight: 100;
font-size: 24px;
}
#box{
max-width: 500px;
margin:0 auto;
margin-top: 100px;
padding:30px;
background-color: #fff;
text-align: center;
box-shadow: 0 1px 3px 0 #dce0e6;
border-radius: 4px;
border: 1px solid transparent;
}
.myButton {
background-color:#72cca7;
background-color:#26367e;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Verdana;
font-size:17px;
padding:10px;
width:30%;
border-radius: 0;
text-decoration:none;
text-align: center;
transition: .3s;
}
.myButton:hover {
opacity: .6;
}
#succes-icon{
width: 100px;
}
</style>
</head>
<body>
<style type="text/css">
.StripeElement {
box-sizing: border-box;
height: 40px;
padding: 10px 12px;
border: 1px solid transparent;
border-radius: 4px;
background-color: white;
box-shadow: 0 1px 3px 0 #e6ebf1;
-webkit-transition: box-shadow 150ms ease;
transition: box-shadow 150ms ease;
}
.StripeElement--focus {
box-shadow: 0 1px 3px 0 #cfd7df;
}
.StripeElement--invalid {
border-color: #fa755a;
}
.StripeElement--webkit-autofill {
background-color: #fefde5 !important;
}
</style>
<script src="https://js.stripe.com/v3/"></script>
<div id="container">
<div class="column-6">
<div id="left-wrapper">
<img id="profile-pic" src="{% static 'images/logo-20may29.png' %}">
<div id="left-wrapper-header">
<h3>Item Purchase</h3>
</div>
<div style="clear:left"></div>
<hr>
<!-- Add form here -->
<form action="{% url 'charge' %}" method="post" id="payment-form">
<div class="form-field-12">
<label for="amount"><b>Pick a product from this dropdown:</b></label>
<select required name="amount" id="amount">
<option selected value="1">$1 CAD ------> TEST</option>
<option value="1">$1 CAD -----> 1 item</option>
<option value="2">$2 CAD -----> 2 items</option>
<option value="3">$3 CAD ----> 3 items</option>
<option value="4">$4 CAD ----> 4 items</option>
<option value="5">$5 CAD ---> 5 items</option>
</select>
</div>
<div class="form-field-6">
<label for="email"><strong>* Enter your email address:</strong></label>
<input required type="email" name="email" placeholder="* email...">
</div>
<div class="form-field-6">
<label for="username"><strong>* Desired username / login name:</strong></label>
<input required type="text" name="username" placeholder="* username...">
</div>
<div class="form-field-6">
<label for="email"><strong>Enter your Dogs name:</strong></label>
<input type="text" name="dog" placeholder="Dogs name...">
</div>
<div class="form-field-6">
<label for="email"><strong>Enter your Favorite beer:</strong></label>
<input type="text" name="beer" placeholder="Favorite beer...">
</div>
<div class="form-field-6">
<label for="country"><strong>* Enter your Country:</strong></label>
<input required type="text" name="country" placeholder="* Country...">
</div>
<div class="form-field-6">
<label for="phone"><strong>* Enter your phone number:</strong></label>
<input type="tel" id="phone" name="phone" placeholder="* Phone..."
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
required>
<small>Format: 123-456-7890</small>
</div>
<div class="form-row">
<label for="card-element">
<strong>Credit or debit card</strong>
</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors. -->
<div id="card-errors" role="alert">
</div>
</div>
<div style="clear:left">
</div>
<hr>
<input id="fjButton" style="background-color:#10a195;height:12px" type="submit" value="Submit Payment" name="">
<hr>
<a href="https://opp.ca" target="_blank" id="fjButton2" style="background-color:#10a195; height:77px" >OPP Homepage</a>
<br><br><br><br>
</form>
</div>
</div>
<div class="column-6">
<div id="right-wrapper">
<img class="form-field-12" src="{% static 'images/Z9999.png' %}">
<h3> We appreciate you!</h3>
<hr>
</div>
</div>
</div>
<script>
// Create a Stripe client.
var stripe = Stripe('pk_live_blahblah');
// Create an instance of Elements.
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
// Create an instance of the card Element.
var card = elements.create('card', {style: style});
// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');
// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
</script>
</body>
</html>
I found this finally... CSS - Why am I not able to set height and width of <a href> elements? and as it states...
"As the others have said, by default is an inline element, and inline elements can't specify a width or height. You can change it to be a block element like this":
a {
display: block;
}
Though it will then display (unsurprisingly) as a block, sitting on its own, outside the flow of the surrounding text. A better solution is to use display: inline-block which might be a best-of-both-worlds solution depending on your situation.
a {
display: inline-block;
}
Height won't have any effect on that <a> tag as it has a display of inline, so you simply need to change its display to something like inline-block to make it obey the set height, like
#fjButton2 {
box-shadow: 0 1px 3px 0 #dce0e6;
font-family:'Fjalla One', sans-serif;;
border-radius: 4px;
background-color: #fff;
border: 1px solid transparent;
color: #44464a;
height: 14px !important;
width: 30%;
padding: 5px;
font-size: 16px;
text-decoration:none;
margin: 0px;
display:inline-block;
}
Hope it helps.

Using CSS, how do I properly align and maintain layout of HTML elements on resize?

In the following HTML/CSS snippet, I'm trying to align the output div below the input field in such a way that they have the same width whenever the window is resized.
Right now the elements get misaligned whenever the viewport dimensions are changed.
How do i refactor and improve my CSS to achieve the desired result?.
I am still very fairly new to web design and development in general and would appreciate it if i could get detailed answers and suggestions. Thanks.
CSS/HTML
html {
font-size: 62.5%;
}
body {
background-color: #c0c0c0;
font-size: 1.6rem;
}
.section {
position: relative;
margin-top: 2rem;
text-align: center;
}
header {
margin-bottom: -2rem;
}
.text-field {
display: inline-block;
background-color: #c0c0c2;
border-top: .4rem solid;
border-color: red;
border-radius: .3rem;
color: black;
width: 50%;
height: 4.2rem;
text-align: left;
text-decoration: none;
font-size: 1.5rem;
min-width: 17.5rem;
}
.btn {
display: inline-block;
background-color: blue;
border-color: black;
border-radius: 3px;
border-style: dotted;
color: white;
padding: 14px 20px;
text-align: center;
text-decoration: none;
font-size: 16px;
}
.output-div {
position: absolute;
display: block;
font-size: 1.4rem;
background-color: #fff;
border-top: .5rem solid;
border-right: .15rem solid;
border-bottom: .15rem solid;
border-left: .15rem solid;
border-color: clack;
border-radius: .3rem;
margin: .5rem auto auto 6.5rem;
min-width: 17.4rem;
padding: .1em;
width: 50%;
height: auto;
text-align: left;
overflow-wrap: break-word;
word-wrap: break-word;
}
<br>
<!-- ✓ Oguntoye -->
<section class="section">
<header>
<h1>Header</h1>
</header>
<div class="user-interaction-area">
<form id="form">
<input class="text-field" type="text" placeholder="Input">
<button class="btn" type="button">Run</button>
</form>
</div>
<!-- output block-->
<div class="output-div">
<kbd class="input-output">Oy</kbd>
<samp class="run-output">vey!</samp>
</div>
</section>
I put your example code inside a codepen. https://codepen.io/melvinhicklin/pen/qyRwXB?editors=1100
From what I could see, the text areas already do a good job of staying the same width.
To align them, I added the following to the .output-div CSS Class:
position:relative;
left:-71px;
display:inline-block;

Radio buttons are checked simultaneously

I've been trying to replicate this page https://vk.com/ You can see that they've changed the standard appearance of radio buttons in the 2nd form.
I succeeded in changing the design of the buttons but now they are all checked simultaneously.
HTML
<form class="form2">
<div class="heading">
<h2>Poprvé na VK?</h2>
<p>Okamžitá registrace</p>
</div>
<input type="text" placeholder="Vaše jméno" required>
<input type="text" placeholder="Vaše příjmení" required class="last-name">
<label class="birth">
<span>Datum narození
<i class="fa fa-question-circle-o"
aria-hidden="true"></i></span>
<input type="date" class="date" required>
</label>
<label>
<span class="gender-head">Pohlaví</span>
<div class="gender">
<input type="radio" id="1-option" name="selector" value="female" class="control">
<div class="button"></div>Žena
<input type="radio" id="2-option" name="selector" value="male" class="control">
<div class="button"></div>Muž
<input type="radio" id="3-option" value="other" name="selector" class="control">
<div class="button"></div>Jiné
</div>
</label>
<button type="submit">Zaregistrovat se</button>
<a href="#">
<i class="fa fa-facebook-square" aria-hidden="true"></i> Přihlásit se přes Facebook</a>
</form>
CSS
form {
width: 290px;
background-color: #fff;
padding: 20px 15px;
float: right;
border: 1px solid #E0E1E3;
font-size: 13px;
border-radius: 3px;
}
.form2 {
margin-right: 10px;
clear: right;
height: 380px;
display: flex;
flex-flow: row wrap;
color: #333436;
justify-content: center;
}
.form2 .heading {
flex-wrap: wrap;
margin: 0 auto;
}
.form2 h2 {
margin-top: 0px;
margin-bottom: 0px;
font-size: 20px;
font-weight: 100;
}
.form2 p {
font-size: 12px;
margin: 2px 0 0 9px;
}
.form2 input {
height: 30px;
border-radius: 3px;
border: 1px solid #DBDCDE;
width: 270px;
margin-top: -15px;
margin-bottom: 0;
}
.form2 .last-name {
margin-top: -15px;
}
.form2 span {
font-weight: 600;
margin-top: -50px;
color: #7A7B7D;
font-size: 13px;
}
.form2 .birth {
margin: -15px 0 0 10px;
}
.form2 .date {
margin-top: 10px;
margin-bottom: -100px;
}
.form2 .gender-head {
margin-left: -10px;
margin-bottom: 20px;
}
.gender {
display: flex;
width: 260px;
margin: 15px 0 0px -10px;
justify-content: space-between;
font-size: 13px;
color: #000;
}
.gender input[type="radio"] {
position: absolute;
top: -9999px;
left: -9999px;
}
.gender .button {
border: 1px solid #A3A4A6;
background: #fff;
border-radius: 50%;
height: 12px;
width: 12px;
margin-top: 2px;
margin-right: -40px;
}
.gender .button:hover {
cursor: pointer;
background-color: #EAEBED;
}
.gender .button::before {
display: block;
content: '';
height: 6px;
width: 6px;
border-radius: 50%;
}
input[type="radio"]:checked ~ .button {
border: 1px solid #5A7CA3;
}
input[type="radio"]:checked ~ .button::before {
background: #5A7CA3;
margin: 3px 2px 2px 3px;
}
::-webkit-input-placeholder {
color: #7A7B7D;
padding-left: 12px;
}
.form2 button {
height: 20px;
}
http://jsfiddle.net/Skidle/u3zj66sd/
I haven't learned JavaScript yet so I'd prefer CSS & HTML only solution. Thanks!
If you look at the radio buttons, by removing the position, you can see that only one is selected, and it is correct for each selection:
The problem lies in the way the presentation works. The CSS code:
input[type="radio"]:checked ~ .button::before // Is wrong.
input[type="radio"]:checked + .button::before // Is right.
Explanation
The code given for the CSS, ~ selector is a sibling selector, which selects all the selectors.
While, what you need is the + selector, which is the immediate or adjacent sibling selector. This selects only one.
Change needs to be done here:
input[type="radio"]:checked + .button::before {
background: #5A7CA3;
margin: 3px 2px 2px 3px;
}
Fiddle: http://jsfiddle.net/0rqu3s2c/
Note: There's an issue when you try to click on the other inputs. So, you might need to check what's blocking it. Please do not use absolute positioning without the desired result.

Having issues placing html buttons side by side in a form

this is my first question here.
I'm having an issue getting my html form buttons side by side.. can somebody take a look and tell me whats wrong? it'd seem like they should by default be placed inline, but I guess that isnt the case.
Here is my html code.
<input type="submit" name="1" formtarget="" value="1">
<input type="submit" name="2" formtarget="" value="2">
<input type="submit" name="3" formtarget="" value="3">
<input type="submit" name="4" formtarget="" value="4">
and here is the CSS for the form input and individual name
#form input {
position: relative;
display: inline;
margin: 0;
padding: 0;
border: none;
outline: none;
-webkit-box-shadow: none;
box-shadow: none;
-webkit-border-radius: 4px;
border-radius: 4px;
text-shadow: none;
line-height: 44px;
-webkit-appearance: none;
}
and this is the same for each button besides the color changes.
#form input[name="1"] {
margin-top: 8px;
height: 44px;
width: 50%;
background: #A901DB;
border-bottom: 1px solid #B404AE;
color: #FFF;
font-size: 18px;
text-align: center;
}
#form input[name="2"] {
margin-top: 8px;
height: 44px;
width: 50%;
background: #A901DB;
border-bottom: 1px solid #B404AE;
color: #FFF;
font-size: 18px;
text-align: center;
}
Can someone help me set this up so that they are inline, side by side?
EDIT: This is what it shows.
http://jsfiddle.net/g01juc2z/2/
you have 4 elements set to width:50% which equals 200% width. Change them to width: 24% (inline-block elements have a natural spacing of 1 or 2px) or less and they will be aligned:
#form input[name="1"] {
margin-top: 8px;
height: 44px;
width: 24%; <---------------
background: #A901DB;
border-bottom: 1px solid #B404AE;
color: #FFF;
font-size: 18px;
text-align: center;
}
FIDDLE
in your css make these change where you write
[name="1"]
replace it with
[type="submit"]
and do not repeat it like a name
and another change is
width:24%;

Styling the input type="file". How do I display the file path?

I have been trying to style an input type="file" field.
My button is styled but I can't seem to figure out how to get the filepath/file to show when the user selects the file to upload.
Can anyone out there help?
.file-upload {
overflow: hidden;
display: inline-block;
position: relative;
vertical-align: middle;
text-align: center;
color: #fff;
border: 2px solid #707070;
background: #A0A0A0;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
text-shadow: #000 1px 1px 4px;
cursor: pointer;
}
.file-upload:hover {
background: #2FA2FF;
}
.file-upload.focus {
outline: 2px solid yellow;
}
.file-upload input {
position: absolute;
top: 0;
left: 0;
margin: 0;
font-size: 12px;
opacity: 0;
filter: alpha(opacity=0);
z-index: -1;
}
.file-upload span {
position: absolute;
top: 0;
left: 0;
display: inline-block;
padding-top: .45em;
}
.file-upload {
height: 38px;
}
.file-upload,
.file-upload span {
width: 160px;
}
.file-upload-status {
margin-left: 10px;
vertical-align: middle;
padding: 7px 11px;
font-weight: bold;
font-size: 16px;
color: #888;
background: #f8f8f8;
border: 3px solid #ddd;
}
<form action="" method="POST" enctype="multipart/form-data">
<div class="fileupload-buttonbar">
<label class="file-upload"><span>Upload....</span><input name="uploadfile" type="file"> </label>
</div>
</form>
Add a change event to the input field, and then just get it's .value.
Example (using jQuery):
$('input[name="uploadfile"]').change(function(){
var fileName = $(this).val();
alert(fileName);
});
DEMO: http://jsfiddle.net/hjNEC/2/
EDIT: Since the input field is hidden, and the file name is part of that, you're gonna have to display fileName on the page yourself.
You can create a new div to show the file path, and use Javascript in the onchange event to update the path displayed in the div whenever the user selects a file.
<form action="" method="POST" enctype="multipart/form-data">
<div class="fileupload-buttonbar">
<input type="file" name="uploadfile" id="uploadfile" style="display:none" onchange="file_path_display.innerHTML=uploadfile.value"/>
<span class="file-upload" onclick="uploadfile.click()" ondragdrop="uploadfile.dragdrop()">Upload....</span>
<div id="file_path_display"></div>
</div>
</form>