I’m trying to get a jquery IE placeholder fix to work. It works fine in IE9 but in IE8 I keep getting an alert…
‘attr(…)’ is null or not an object
This is the code I am using. Am I doing something wrong?
<!--[if lte IE 9 ]>
<script>
// IE Placeholder Fix
$(function() {
if ($('input[placeholder]').attr('placeholder').val() != '') {
var ph = $('input[placeholder]').attr('placeholder');
$('input[placeholder]').blur();
if ($('input[placeholder]').attr('value').val() == 0) {
$('input[placeholder]').attr('value', ph);
$('input[placeholder]').css('color','#999999');
};
$('input[placeholder]').click(
function() {
$(this).css('color','#000000');
if ($(this).attr('value') == ph) {
$(this).attr('value','');
}
});
}
});
</script>
<![endif]-->
UPDATE:
Using the comments below I came up with…
<!--[if lte IE 9 ]>
<script>
// IE Placeholder Fix
$(function() {
if ($('input[placeholder]').length != 0) {
var ph = $('input[placeholder]').attr('placeholder');
$('input[placeholder]').blur();
if ($('input[placeholder]').attr('value') == '') {
$('input[placeholder]').attr('value', ph);
$('input[placeholder]').css('color','#999999');
};
$('input[placeholder]').click(
function() {
$(this).css('color','#000000');
if ($(this).attr('value') == ph) {
$(this).attr('value','');
}
});
}
});
</script>
<![endif]-->
Calling a method or property on a null object will result in errors of the sort you are experiencing. If you are not sure if an object is defined, test this before referring to any children.