0

I'd like to be able to set default placeholders for string really easily like this

someString.placeholder("waiting for text") 

Which could be used like this...

$("p.some-class").html( someString.placeholder("waiting for text") );

so if 'someString' is empty, the user sees "Waiting for text", but if the string has a length greater than 0, they see the actual string.

I've tried to extend the String object like this

String.prototype.placeholder = function(placeholder){

    return (this.length > 0) ? 
    this : 
    "<span class=\"placeholder-string\">" + placeholder + "</span>";

}

but that doesn't appear to be working. Any thoughts anyone?

Here's a JSFiddle for anyone who's interested.

6
  • How are you inputting a string into a span tag? Commented Oct 18, 2011 at 14:52
  • Why? Because I want to style it with css. So placeholders are knocked back a bit. Commented Oct 18, 2011 at 14:53
  • 3
    How about using a logical OR: someString || "waiting for text" ? Commented Oct 18, 2011 at 14:54
  • 1
    The placeholder method you defined is called placeholder not placeHolder. I can't see any other problems with it. Commented Oct 18, 2011 at 14:54
  • placeholders for what? how the heck are they putting text in? some context and html maybe? and a demo too? Commented Oct 18, 2011 at 14:54

1 Answer 1

1

It appears that this is a String object, not the primitive version. You'd have to convert it into a primitive. Also you can eliminate > 0:

String.prototype.placeholder = function(placeholder){

    return this.length ? 
    this + "" : 
    "<span class=\"placeholder-string\">" + placeholder + "</span>";

}
Sign up to request clarification or add additional context in comments.

Comments

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.