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">
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...
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.
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.
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With