Font awesome icon font as a placeholder in input after adding class with jquery

Acidon

I am trying to use font-awesome icons as a placeholder in search input field.

Jsfiddle examples

I use corresponding html entity as a placeholder, then use pseudo class to style placeholder with correct font-family (example 2 in jsfiddle):

HTML:

<div class="wrapper">
    <input class="icon" type="text" placeholder="&#61442;" />
</div>

CSS:

.wrapper {
    font-family:'arial', sans-serif;
}
input.icon {
    padding:5px;
}
input.icon::-webkit-input-placeholder {
    font-family:'FontAwesome';
}
input.icon::-moz-placeholder {
    font-family:'FontAwesome';
}
input.icon::-ms-input-placeholder {
    font-family:'FontAwesome';
}

It works as expected.

The problem starts when I try to add input class and placeholder via jquery (example 1 in jsfiddle):

JS:

$(document).ready(function() {
$('.wrapper input:first').addClass('icon');
$('.wrapper input:first').attr("placeholder", "&#61442;");
});

It doesn't apply font-family to input placeholder and just shows entity text instead.

I tried to mix things around and finally giving up. Please help!

Jsfiddle examples

P.S.: The rout of adding :before with font content to input wrapper in css will not work for my particular case.

Filipe

You should parse the hash first. You can use $.parseHTML:

$('.wrapper input:first').attr("placeholder", $.parseHTML("&#61442;")[0].data);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Font Awesome icon inside text input element

From Java

Use Font Awesome Icon in Placeholder

From Dev

Put an icon after list item with :after and font awesome

From Dev

Changing Font Icon in WPF using Font Awesome

From Dev

jQuery Accordion change font awesome icon class on click

From Dev

font awesome does not appear even after adding class name

From Dev

Font awesome icon and text in input submit button?

From Dev

How to add Font Awesome icon into <input> button?

From Dev

How to insert font awesome icon into text input?

From Dev

Font Awesome and text both in placeholder

From Dev

Styling plaintext after Font Awesome icon

From Dev

Adding font awesome icon inline yet superscript with other element

From Dev

Select2 Font Awesome Icon on Placeholder

From Dev

Font Awesome icon inside of HTML input with Angular working

From Dev

Adding font-awesome icon inside a input field when using simple-form

From Dev

jQuery removes Font Awesome Icon

From Dev

How do I add a Font Awesome icon to file input field

From Dev

Set font awesome icon into jquery

From Dev

Font Awesome Icon Alignment

From Dev

Font awesome icon placed top right corner of input within table

From Dev

Font Awesome icon in React

From Dev

Changing Font Icon in WPF using Font Awesome

From Dev

font awesome does not appear even after adding class name

From Dev

AngularJs + jQuery DatePicker directive + font awesome icon

From Dev

How to position an Awesome Font Icon after a text

From Dev

Font Awesome Icon Alignment

From Dev

Add Font Awesome Icon To Class

From Dev

How to input block of text next to Font Awesome icon in CSS?

From Dev

How to get font awesome icon into input type text

Related Related

HotTag

Archive