Skip to content

Instantly share code, notes, and snippets.

@TCotton
Created July 12, 2011 13:51
Show Gist options
  • Save TCotton/1078011 to your computer and use it in GitHub Desktop.
Save TCotton/1078011 to your computer and use it in GitHub Desktop.
IE Placeholder JavaScript replacement
if (!Modernizr.input.placeholder) {
// always declare javascript variables at the top of the script
var $elements, $i, $x, $len, $formInput, $inputField, $form;
$form = document.getElementById('contact-us').elements;
$i = 0;
//set placeholder values for internet explorer
for ($len = $form.length; $i < $len; $i += 1) {
if ($form[$i].getAttribute("placeholder") != undefined) {
$form[$i].value = $form[$i].getAttribute("placeholder");
} // if not undefined
} // end for loop
$inputField = document.forms.contactUs.getElementsByTagName('input');
$x = 0;
// loop through input areas to make sure placeholder disappears on focus
for ($len = $inputField.length; $x < $len; $x += 1) {
// Don't change the submit input!
if ($inputField[$x].type !== "submit") {
// Give placeholder text a grey colour
$inputField[$x].style.color = "#999";
//Delete the placeholder text and change CSS text colour to black on user focus
$inputField[$x].onfocus = function () {
this.value = "";
this.style.color = "#000";
};
}
} // if not undefined
} // end modernizr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment