IE: None Firefox: 3.7+ Safari: 4+ Chrome: 4+ Opera: 11+ iPhone: 4+ Android: None
With this in mind, we can see that, in my mind, the most troublesome browser IE doesn't support placeholder at all, neither does the Android browser. Thankfully, this can be fixed using jQuery. There are also older versions of modern browsers that don't support it, this jQuery code will work for those browsers also
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head> <body> </body> </html>
Now that we have the basic outline done we can begin making our simple form. Also, in this tutorial we won't be doing any styling since it's not needed (actually that's a lie, we'll be doing changing the color of font with some jQuery magic). We're basically done with the HTML layout, but you can add a lot more if you want. Before we can do anything with jQuery, we need to add the script to our head element. I will be using Google's CDN hosted version of the minimized version of jQuery 1..6.2 that can be found here. Here is my code for adding it to the head element.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
Now, that we have jQuery being linked to our HTML document, let's get the form done, shall we.
<form> <input type="text" id="placeholder-fallback" placeholder="Placeholder Tutorial" /> </form>
Now, there isn't much to explain there, we create our form with one input field. It's a text input with the id of “placeholder-fallback”, we will need it for the jQuery. We also set the placeholder attribute and that's about it. You can add other form elements, but this is all that we need for the purpose of this tutorial. Now we're ready to dip our feet into jQuery. You don't have to know to much Javascript for this tutorial, but I do suggest that you understand basic programming. Like setting a variable to something and creating if statements. We're going to embed this into our HTML document, but you may use an external Javascript file. Let's set up our script tags in our head element.
<script type=”text/javascript”> </script>
Okay, now I'm going to cut up this wall of jQuery so that it's easier to understand. I'll also explain each block. To start we need to add this little bit of jQuery.
$(function() {
})
All of our jQuery will go in that function. Also, just so you guys know when I say jQuery or Javascript in this tutorial, I mean both at the same time. Alright. Now let's create another function to see if the placeholder attribute is being recognized inside the input, if it isn't being recognized then we know it's not supported, if it is recognized then it's supported. Easy isn't it? Well, here's the function.
function placeholder_supported() {
var input = document.createElement("input");
return('placeholder' in input);
}
var placeholder_supported = placeholder_supported();
Alright so we create a function with a name that I thought would be pretty obvious. Then we create a variable called input and create an input element. We then return the placeholder attribute inside the input. We then create a variable outside the function that is equal to the function. When ever we use that variable, we're checking whether or not placeholder is supported or not. Okay, we are now going to check if it's false. If placeholder is returned false, it isn't supported, if it's true, then it is supported. If it's true, we're not going to do anything and continue to display the HTML 5 placeholder attribute as it is. If it isn't, well I'll just show you the code.
if (placeholder_supported == false) {
// There isn't any HTML5 placeholder support so we're going to set the value to what we want it to be.
$('#placeholder-fallback').val('Placeholder Tutorial');
$('#placeholder-fallback').css({'color' : '#C0C0C0'});
}
If the placeholder attribute isn't supported we set a value for the #placeholder-fallback, remember that was the id we set to our input field. You may, if you want, create a function that passes the id of the input, but for simplicity, we will hard code the ids into the functions and if statements. We also set the color of that form element to gray. This will fix the placeholder when you load the page, but what about when you focus on the form or leave the form. It'll stay the same way. Not disappearing or reappearing. To fix that we need to use the focus and blur functions of jQuery. Here's the focus function.
$('#placeholder-fallback').focus(function() {
if (placeholder_supported == false) {
if ($(this).val() == 'Placeholder Tutorial') {
$(this).val('');
$('#placeholder-fallback').css({'color' : '#000000'});
}
}
});
Okay, we're performing the focus function on the #placeholder-fallback form element. We then check if the placeholder attribute isn't supported again and if it isn't we check the value using $(this), basically referencing the element, and see if the value is what we have it equal to. If it is, we clear the value and set the color of the text back to black. Now, when you click on on the form element the text disappears, but if you leave the form element, the placeholder doesn't reappear. Let's fix that with the blur function.
$('#placeholder-fallback').blur(function() {
if (placeholder_supported == false) {
if ($(this).val() == '') {
$(this).val('Placeholder Tutorial');
$('#placeholder-fallback').css({'color' : '#C0C0C0'});
}
}
});
This is basically doing the opposite of what we did when using the focus function. We use the blur function of jQuery on the #placeholder-fallback form element. We then check if the placeholder attribute isn't supported again and if it isn't we check the value using $(this). If it is empty, we set the value to what we have the placeholder set to and change the color back to gray. Now, the form should work like it does when using the HTML 5 placeholder attribute.
Yay! We did it guys! We outsmarted older browsers. I hope this tutorial taught you how to use the placeholder attribute and how to create a fallback with jQuery. I'm sure that there are other ways to accomplish this and ways to optimize it. If you guys got any suggestions or questions, leave a comment below. Thanks for reading!
As always here is a zip of the tutorial
placeholder tutorial.zip (854bytes)
Number of downloads: 311
This post has been edited by EnvXOwner: 17 September 2011 - 04:23 PM






MultiQuote




|