Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, 9 October 2013

How to make a nice file upload button with HTML & CSS

We have all seen the horrible file upload boxes, we all know what they look like.. and they all differ slightly from browser to browser.

There are lots of hacks out there to emulate a button click to launch the file input dialog, or to position the input box transparent ontop of a beautified button, but all of these are limited to the browser support and some browsers require hacks.

I want to show you the simplest way I can think of..

Here's how it looks:




Ok that's cool, but how exactly does it work?



<style>
.upload_box {
    overflow:hidden;
    position:relative;
}

#select_file {
    position:absolute;
    left:-300px;
}

label {
    cursor:pointer;
    font-weight:bold;
}
</style>
<div class="upload_box">
    <input id="select_file" type="file" />
    <label for="select_file" id="selected">To select your file click here!</label>
</div>
>And that's all there is to it.. Let me explain.. the good thing about the label tag is it links to the input element that you would like to focus on.. the container ( .upload_box ) has the css attribute overflow:hidden; so we basically hide the input element off to the left, which essentially "hides" the input element, so when you click on the label, it triggers the input dialog. Because this is a label, you can style it to however you like.. this is how I have styled it for one of my clients:






So, now we've covered how to do it.. there's one more thing you should know, once you select the file it won't display the file name, so to do this we do need a small piece of Javascript.. This is the Javascript:


<script>
function fileName()
{
  var _file = document.getElementById('select_file');
  document.getElementById('selected').innerHTML = _file.value.replace(/C:\\fakepath\\/i, '');
}
</script>

To make this work, you need to attach fileName() to the input field, to do this simply add onchange="fileName()" like so:


<input id="select_file" type="file" onchange="fileName()" />

I hope this has helped!

And here's a working DEMO



Thursday, 22 March 2012

Custom select dropdown with CSS and JS

Dropdowns have been a major pain in the backside in the past when it comes to styling of them, the default look doesn't always suite your website theme.


Customization is very limited, but it's not very difficult to change the style of the dropdown with CSS and javascript.


Lets turn this:






Into this:






Step 1
The HTML - This is simple, basically we have a div which holds the select element, it also holds the span element:


<div class="selection">
    <select class="selectbox" onchange="changeValue(this.value);">
        <option>test select</option>
        <option>test select 2</option>
        <option>test select 3</option>
        <option>test select 4</option>
        <option>test select 5</option>
    </select>
    <span id="selector">test select</span>
</div>


Step 2
The CSS - this tells the select element to become invisible, and tells the span what to look like and to sit below the selectbox.


<style>
.selection { position:relative; }

.selectbox,#selector {
 position:absolute;
 top:0;
 left:0;
 font-family:Arial, Helvetica, sans-serif;
 font-size:15px;
}

.selectbox {
 width:220px;
 padding:5px;
 height:30px;
 opacity:0;
 z-index:5;
}

#selector {
 background-image:url(dropdown.jpg);
 background-repeat:no-repeat;
 padding:5px 43px 5px 10px;
 overflow:hidden;
 width:162px;
 height:20px;
 color:#fff;
}
</style>


Perfect, as you can see from the CSS, the span with the id of #selector uses a background image called dropdown.jpg - this gives the coloured background and the arrow, you can make your own one. - this image is 215x30px if you change the size of it make sure you adjust the CSS accordingly, basically fiddle around with the #selector width and padding. You may also need to change the .selectbox width and padding if you experience difficulty clicking the dropdown once you've changed the size.


Step 3
The Javascript - this is just to update the span with the selected dropdown option.


You can do this with the jQuery library (if you already use it on your website), or using built in javascript, both work fine:


jQuery


<script>
function changeValue(val)
{
 $('#selector').html(val);
}
</script>

Javascript


<script>
function changeValue(val)
{
 document.getElementById('selector').innerHTML = val;
}
</script>


So there you have it, the complete code is below:


Complete Code


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Custom Select box</title>
<script>
function changeValue(val)
{
 document.getElementById('selector').innerHTML = val;
}
</script>
<style>
.selection { position:relative; }

.selectbox,#selector {
 position:absolute;
 top:0;
 left:0;
 font-family:Arial, Helvetica, sans-serif;
 font-size:15px;
}

.selectbox {
 width:220px;
 padding:5px;
 height:30px;
 opacity:0;
 z-index:5;
}

#selector {
 background-image:url(dropdown.jpg);
 background-repeat:no-repeat;
 padding:5px 43px 5px 10px;
 overflow:hidden;
 width:162px;
 height:20px;
 color:#fff;
}
</style>
</head>

<body>
<div class="selection">
    <select class="selectbox" onchange="changeValue(this.value);">
        <option>test select</option>
        <option>test select 2</option>
        <option>test select 3</option>
        <option>test select 4</option>
        <option>test select 5</option>
    </select>
    <span id="selector">test select</span>
</div>
</body>
</html>

Thursday, 26 May 2011

How to remove the fonts.com footer banner

If you use webfonts.fonts.com for their custom web fonts API, and like me, you don't like their default footer banner.
You've probably wanted to remove it and replace it with your own custom, much slicker banner which works nicely with your website.
I recently had this problem and decided to do something about it.

How they do it
The javascript file you include in the head of your html document loads their custom css and font, it then waits for 6 seconds to load the banner, this ensures all your other scripts have loaded, leaving nothing left but their banner to be placed neatly at the bottom of the page looking hideous.

I am going to give two examples here to removing this banner, the first will be using the jQuery library.

Using the jQuery library
Below is the complete code, it would be nice if you could leave my simple credits in there, Basically what happens is we count how many div tags have been loaded into the page and then we keep checking for another div to be added at the bottom of the page, once this happens we change the css attribute of display to none, which hides the banner.

/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

/* when the document is ready start */
$(document).ready(function(){
   /* set variables */
 var count,newcount;
   /* count total divs loaded on the page */
  count = $('div').length;
 function checkwebFontDiv() {
    /* count total divs loaded on the page */
  newcount = $('div').length;
    /* if the total divs has increased by 1 then continue */
  if(newcount == count+1){
     /* hide the last div (the fonts.com banner div) */
   $('div:last').css("display","none");
    /* otherwise check again */
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   /* start checking for the new div */
 checkwebFontDiv();
});

This is an example of how to implement this into your webpage.
First of all you need to download the jQuery library from here jquery.com (latest version).
Secondly you need to make sure you place this inside your head tags
<html>
<head>
<title>My test page hiding fonts.com banner</title>
<script src="javascript/jquery.1.5.1-min.js" type="text/javascript"></script>
<script type="text/javascript">
/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

/* when the document is ready start */
$(document).ready(function(){
   /* set variables */
 var count,newcount;
   /* count total divs loaded on the page */
  count = $('div').length;
 function checkwebFontDiv() {
    /* count total divs loaded on the page */
  newcount = $('div').length;
    /* if the total divs has increased by 1 then continue */
  if(newcount == count+1){
     /* hide the last div (the fonts.com banner div) */
   $('div:last').css("display","none");
    /* otherwise check again */
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   /* start checking for the new div */
 checkwebFontDiv();
});
</script>
<head>

and that's it.

 
Using javascript
Below is the complete code, it would be nice if you could leave my simple credits in there, Basically what happens is we count how many div tags have been loaded into the page and then we keep checking for another div to be added at the bottom of the page, once this happens we change the css attribute of display to none, which hides the banner. The difference between this and the example above is that you don't need the jQuery library loaded, you also need to place this at the bottom of your webpage, just before the closing body tag.

<script>
/* http://dannyscodesnippets.blogspot.com 2011 removing webfonts.fonts.com banner */

 var count,newcount,xDivs;
 xDivs =  document.getElementsByTagName('div');
   // count total divs loaded on the page
 count = xDivs.length;
 function checkwebFontDiv() {
    // count total divs loaded on the page
  newcount = xDivs.length;
    // if the total divs has increased by 1 then continue
  if(newcount == count+1){
     // hide the last div (the fonts.com banner div)
   xDivs[count].style.display='none';
    // otherwise check again
  } else {
   setTimeout(checkwebFontDiv,5); 
  }
 }
   // start checking for the new div
 checkwebFontDiv();

</script>
</body>
</html>

That's it!
Any questions please comment.