Results 1 to 8 of 8

Thread: Having JS add/append HTML instead of assigning a class

  1. #1
    Join Date
    Apr 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having JS add/append HTML instead of assigning a class

    Okay, sorry for the lousy title. The problem I am running into is with a javascipt which unobtrusively attaches a behavior to all elements that have the rel attribute of "popup." I have it so that it assigns a class and in doing so, it (the class) places a background image to the right of an external link to indicate the link will open in a new window. It works great but there is a problem with the way Explorer handles the icon--if the link text goes to two lines, the placement of the icon gets all messed up. There is a way around this--to add a conditional comment just for IE (zoom: 1)--which forces any two line text link to the next line so that the text remains all on one line. It's just not going to work for this particular site but I have found a way to add an icon through HTML that Explorer doesn't bork. My problem is that I don't know how to say to the javascript:

    "When you find all elements that have the rel attribute of 'popup,' instead of doing this:

    Code:
    // assigns a class so I can attach zoom conditionally for IE to solve 2 line problem
            popups[i].className = "popup";
            }
    ...add/append the following HTML and do it so that it ends up right before the closing a tag (like this: <a href="#" rel="popup">Link Text<span class="icon">&nbsp;</span></a>):

    Code:
    <span class="icon">&nbsp;</span>
    Is there a javascript pro who can point me in the right direction?

    (I'm using an adaptation of the script made available here: http://www.accessify.com/features/tu...perfect-popup/)

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    popups[i].appendChild(document.createElement("span"))
        .appendChild(document.createTextNode("\a0"));
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Rats...I don't understand your answer. What's the "\a0" stuff?

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Actually the value of the text node is 'a0' and the '\' is used to escape any special character to literal and vice versa.

    Twey combined four operations in a single statement. If you are having difficulty you can split up these operation into multiple statements which will do exactly what he achieved using a single statement

  5. #5
    Join Date
    Apr 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi. Okay, I get that the non-breaking space has to be written as \u00A0 but what about assigning the class "icon" to the span? Where do you do that in the statements?

    Code:
    <span class="icon">&nbsp;</span>

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Try any of the following method;
    Code:
    popups[i].appendChild(document.createElement("span")).appendChild(document.createTextNode("\a0"));
    popups[i].lastChild.className = 'icon';
    Code:
    var span = document.createElement("span");
    span.className = "icon";
    span.appendChild(document.createTextNode("\a0"));
    popups[i].appendChild(span);

  7. #7
    Join Date
    Apr 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much! Two for the price of one!

    But can you tell me--is one approach better than the other?

    Also, out of curiosity, how come you're writing the non-breaking space as "\a0" instead of "\u00A0" ? Is that some type of shorthand? It doesn't seem to work within the script and is output as "a0"

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, it was my error. \XX is used in some languages, but in JS it has to be \uXXXX.
    Code:
    popups[i].appendChild(document.createElement("span"))
        .appendChild(document.createTextNode("\u00A0"))
        .parentNode.className = "icon";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •