How do I dynamically create an <option> in JavaScript that contains an HTML entity (— … «)?

陌路散爱 提交于 2019-11-30 19:01:25
Adam Douglass

I just realized I could use a Unicode JavaScript escape:

e.options[0] = new Option('\u2014 Select One \u2014', '');

You don't need to escape the entity - it works like this:

function selectOne() {
      var e = document.getElementById('test');
      e.options[0] = new Option('— Select One —', '');
}

text property doesn't get unescaped, as it is meant to be taken literally. If you use innerHTML, the entities get converted to corresponding characters.

e.options[o].innerHTML = '&mdash; Select One &mdash;';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!