SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: DOM ampersands

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM ampersands

    I have the following line of code in my javascript

    Code:
    var myval = 'test';
    if (document.getElementById('my_checkbox').checked) myval += '&';
    document.getElementById('my_span').firstChild.nodeValue = myval;
    ......
    (html part)
    <span id="my_span">xxx</span>
    But instead of changing the span to "test&", it changes it to "test&amp;". I know I could just use myval += '&'...but I want to use other symbols...

    i.e., I'm using the following 2 codes for musical flat and sharp signs.
    ♭ ♯
    Is this the best way (i.e. will the symbols show up on all computers), or is there a better way (other than using an image?).

    Many thanks for your help!

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use myval += '&'.

    One would perhaps think that you should use document.createEntityReference() for this, but according to the DOM Level 2 (Core) specification:
    Note that character references and references to predefined entities are considered to be expanded by the HTML or XML processor so that characters are represented by their Unicode equivalent rather than by an entity reference.
    To add the flat and sharp signs, you can use JavaScript Unicode string escapes: "\u266d" and "\u266f".
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •