简体   繁体   中英

Escaping automatic encoding of & character in JavaScript

I want to create a form button whose input value is a font icon. FontAwesome in this case. So I use a entity such as this for the input value:  In plain HTML styled in CSS with font-family: FontAwesome , everything works as expected:

<input type="submit" class="btn fa-input" value="&#xf043; Input">

However, I want to create this element using JavaScript. In that case the & character seems always to end up being encoded so my entity comes out as &#xf107; Needles to say, that prints out literally instead of the font icon. How do manage to not have that & character encoded?

Below is the Javascript I am using

var button = utility.dom.createElement('input', {
  'type': 'button', 
  'className': 'fonticon', 
  'value': '&#xf107;',
  'title': 'bla bla bla',
  'tabIndex': 1000
});

and the CSS

.fonticon {
  font-family:FontAwesome;  
}

If you are setting the value using JavaScript instead of hardcoding it into your HTML, use a JavaScript escape sequence instead, such as \ . Your code for the button's value should change to:

'value': '\uf107',

By the way, U+F107 doesn't appear to be a valid character: http://www.fileformat.info/info/unicode/char/f107/index.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM