65

html5 supports the placeholder attribute on input[type=text] elements, but I need to handle non-compliant browsers. I know there are a thousand plugins out there for placeholder but I'd like to create the 1001st.

I am able to get a handle on the input[placeholder] element but trying to get the value of the placeholder attribute is returning undefined - $("input[placeholder]").attr("placeholder").

I'm using jquery 1.6.2.

Here is the jsfiddle. I modified the code to work in a browser that is html5 compatible just for testing purposes.

html

<input type="text" name="email" size="10" placeholder="EMAIL ADDRESS">

jquery

function SupportsInputPlaceholder() {
    var i = document.createElement("input");
    return "placeholder" in i;
}

$(document).ready(function(){
    if(!SupportsInputPlaceholder()) {
        //set initial value to placeholder attribute
        $("input[placeholder]").val($("input[placeholder]").attr("placeholder"));

        //create event handlers for focus and blur
        $("input[placeholder]").focus(function() {
            if($(this).val() == $(this).attr("placeholder")) {
                $(this).val("");
            }
        }).blur(function() {
            if($(this).val() == "") {
                $(this).val($(this).attr("placeholder"));
            }
        });
    }
});

Thanks for any and all help, B

0

2 Answers 2

84

You need some form of iteration here, as val (except when called with a function) only works on the first element:

$("input[placeholder]").val($("input[placeholder]").attr("placeholder"));

should be:

$("input[placeholder]").each( function () {
    $(this).val( $(this).attr("placeholder") );
});

or

$("input[placeholder]").val(function() {
    return $(this).attr("placeholder");
});
Sign up to request clarification or add additional context in comments.

9 Comments

Wouldn't $("input[placeholder]").val(function() { return $(this).attr("placeholder"); }); be better?
Thanks Dennis. when i set a breakpoint on each i found that it's not even entering the function, like it's not finding any elements that match that selector. But when I put a watch on $("input[placeholder]").val() I see "" and if I give the element an initial value I'll see the value.
@Richard Yeah, that's a bit better. Probably not too much difference thought. bflemi3, What browser are you using? Your code works in Chrome and IE7
I'm using firefox 5 and I've commented out the if statement that checks if the browser supports the placeholder attribute since firefox 5 supports html5.
I can't reproduce the problem. The code you have provided works fine. Can you make a jsfiddle/jsbin that shows it?
|
13

You can also do this by passing function with onclick event

<a onclick="getColor(this);" color="red">

<script type="text/javascript">

function getColor(el)
{
     color = $(el).attr('color');
     alert(color);
}

</script> 

1 Comment

onlick...hmmm...maybe should be onclick :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.