Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to check if placeholder is supported?

I want to use the HTML5 "placeholder" attribute in my code if the user's browser supports it otherwise just print the field name on top of the form. But I only want to check whether placeholder is supported and not what version/name of browser the user is using.

So Ideally i would want to do something like

    <body>       <script>             if (placeholderIsNotSupported) {              <b>Username</b>;            }        </script>     <input type = "text" placeholder ="Username"> </body> 

Except Im not sure of the javascript bit. Help is appreciated!

like image 342
algorithmicCoder Avatar asked Nov 25 '11 00:11

algorithmicCoder


People also ask

Can use as a placeholder when has one or more values but don't know what they are?

A variable is a placeholder for an unknown quantity.

How does a placeholder work?

The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format). The short hint is displayed in the input field before the user enters a value.

What is placeholder shown in CSS?

The :placeholder-shown pseudo-class selects the input element itself when placeholder text exists in a form input. Think of it as a nice way to distinguish between inputs that are currently showing placeholder text versus those that are not.

What is a placeholder tag?

Definition and Usage The placeholder attribute specifies a short hint that describes the expected value of a input field / textarea. The short hint is displayed in the field before the user enters a value.


2 Answers

function placeholderIsSupported() {     var test = document.createElement('input');     return ('placeholder' in test); } 

I used a jQuery-ized version as a starting point. (Just giving credit where it's due.)

like image 113
Trott Avatar answered Sep 21 '22 14:09

Trott


Or just:

if (document.createElement("input").placeholder == undefined) {     // Placeholder is not supported } 
like image 30
Nikita Gavrilov Avatar answered Sep 22 '22 14:09

Nikita Gavrilov