I want to change the placeholder attribute trying attr()
using it this way:
$(this).attr("placeholder", " Mandatory field");
But the result is not the Font-Awesome icon , it literally looks looks like this:
" Mandatory field"
Nevertheless, if I put
<input type="text" class="mandatory_field form-control" id="affiliation" placeholder=" Mandatory field"/>
with CSS
.mandatory_field {
font-family: FontAwesome,"Helvetica Neue",Helvetica,Arial,sans-serif;
}
It works, but I need to know how to get those results dynamically with jQuery.
Thanks in advance.
The “placeholder” attribute controls the text that will be shown as a placeholder in an input area. This attribute is passed as the first argument to the attr() method. The required text to be set is passed as the second argument. This will change the placeholder text of the element to the new text.
Use placeholder="" in your input. You can find unicode in FontAwesome page http://fontawesome.io/icons/ . But you have to make sure add style="font-family: FontAwesome;" in your input. Save this answer.
If you wanna use Javascript then you can use getElementsByName() method to select the input fields and to change the placeholder for each one... see the below code... +1 for going native without using external libraries ...
Font Awesome SVG with JavaScript is compatible with jQuery but requires some additional understanding and planning.
font-family:FontAwesome
You'll need the specific CSS code for the icon prefixed with a \u
. Font-Awesome documents only class and unicode. Fortunately, this site has what you need. Here's what it should be in jQuery:
$('.mandatory_field').attr('placeholder', '\uf0a4 Mandatory field');
The only difference between CSS and JavaScript font entities is the u
in the second character position for JavaScript.
In the demo, I also added some styles with the ::placeholder
pseudo element as a bonus. You can style your placeholders without affecting the user input style.
$('.mandatory_field').attr('placeholder', '\uf0a4 Mandatory field');
/* With the exception of font-family:FontAwesome everything
|| is optional.
*/
input {
font: inherit
}
.mandatory_field::-webkit-input-placeholder {
/* Chrome/Opera/Safari */
color: red;
font-family: FontAwesome;
font-variant: small-caps;
}
.mandatory_field::-moz-placeholder {
/* Firefox 19+ */
color: red;
font-family: FontAwesome;
font-variant: small-caps;
}
.mandatory_field:-ms-input-placeholder {
/* IE 10+ */
color: red;
font-family: FontAwesome;
font-variant: small-caps;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css">
<input type="text" class="mandatory_field form-control" id="affiliation">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You can use a data
attribute to store the awesome font symbol you want and use it combined with the text $input.data('placeholder') + ' Mandatory field'
.
Code example:
var $input = $('#affiliation'),
placeholder = $input.data('placeholder') + ' Mandatory field';
$input.attr('placeholder', placeholder);
.mandatory_field {
font-family: FontAwesome,"Helvetica Neue",Helvetica,Arial,sans-serif;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" data-placeholder="" class="mandatory_field form-control" id="affiliation">
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