/    Sign up×
Community /Pin to ProfileBookmark

setting values to html entities through the DOM

In HTML,

[code]<input type=”button” value=”&rarr;”>[/code]

gives me a button which renders with an arrow. I want to be able to set the value of a button to such an arrow dynamically as well. So

[code]
var buttonEl = document.createElement(‘input’);
buttonEl.type = ‘button’;
buttonEl.value = ‘&rarr;’;
[/code]

Unfortunately, this does not work. I get instead a button which renders with the ASCII “&rarr;”

Is there a different javascript string literal which will render as the HTML entity? Or how is one supposed to achieve this? Thanks in advance.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@letheauthorSep 21.2007 — In HTML,

Is there a different javascript string literal which will render as the HTML entity? Or how is one supposed to achieve this? Thanks in advance.[/QUOTE]


Found the answer. Use eg 'u2190'. 2190 is the hexadecimal codepoint for the right arrow html entity.
Copy linkTweet thisAlerts:
@toicontienSep 21.2007 — Here's a UTF-8 reference for arrow entities:

http://formex.publications.europa.eu/formex-4/physspec/formex-4-character-encoding-c12.htm

The numbered entity reference is [&]#8594; You can use that number with the String.fromCharCode() function to return an actual character:
String.fromCharCode(8594);
Copy linkTweet thisAlerts:
@mrhooSep 22.2007 — /*You can convert any html string, with any number of entities, to javascript unicode with innerHTML and an empty span element.

Using unicode is simpler for the computer, but the occasional '&rarr;' is easier on me.

*/

[CODE]function echoText(str){
var spanEl= document.createElement('span');
spanEl.innerHTML= str;
return spanEl.firstChild.nodeValue;
}[/CODE]


//test cases:

var inputEl= document.createElement('input');

var buttonEl=document.createElement('button');

document.body.appendChild(inputEl);

document.body.appendChild(buttonEl);

[B]var str= echoText('This way &rarr; or that way&larr;');[/B]

[B]inputEl.value= str;

buttonEl.appendChild(document.createTextNode(str));[/B]
×

Success!

Help @lethe spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...