Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show placeholder text as label in Desktop [duplicate]

I want to show placeholder in mobile view and hide placeholder in desktop view. How could I do this?

End inAnd mobile view should look like this:

<label>Enter Name:</label>
<input type="text"/>

And mobile view should look like this:

<input type="text" placeholder="Name">
like image 933
Tzof Avatar asked Aug 20 '17 06:08

Tzof


People also ask

Can we use placeholder in label?

Placeholder Attribute Is Not A Label! The placeholder should only be used as a brief example of the text to be entered. Besides inconsistent support for screen readers, using a placeholder as an input label can create usability problems and issues for those with cognitive impairments...

Why is the use of placeholder text as a form label bad for accessibility?

In recent years, placeholders are being used to provide visible labels for the form fields, which is a bad user experience and accessibility practice. This is because placeholders disappear once the user inputs the data into the form controls, the user does not have any idea of what the form field is referring to.

How do you display placeholder based on condition?

You can use the conditional operator ?: in JavaScript to set the attribute placeholder either to a value or to an empty string. Setting the placeholder to an empty string should be the same as having it not set for most common use-cases.


1 Answers

With jquery :

$(document).ready(function(){
    $(window).resize(function(){
        var wid = $(this).width();
        if((wid > 300) && (wid < 400)) {
            $('input[type="text"]').attr('placeholder','name');
            $('label').hide();
        }
        else {
            $('input[type="text"]').attr('placeholder','');
            $('label').show();
        }
    })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<label>Enter Name:</label>
<input class="int" type="text"/>
like image 116
Ehsan Avatar answered Sep 29 '22 12:09

Ehsan