Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: get by id and write??

  1. #1
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy get by id and write??

    how would i use the get element by id "star" and make it find a * and replace the * with an image?

    Help appreciated

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    What you are saying cannot be done, but that is probably just because you haven't explained clearly, in layman's terms, what you want to do. There is no getElementById('*') that I know of, * is not technically a valid id. However, I might just be being too strict to standards in interpreting what you want. Here is one idea that would fit your question, if I've understood it:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <span id="*">HI</span>
    <script type="text/javascript">
    var star=document.getElementById('*');
    var I=document.createElement('img');
    I.src='http://www.google.com/intl/en_ALL/images/logo.gif';
    star.parentNode.replaceChild(I,star);
    </script>
    </body>
    </html>
    But, as I say, it is invalid. As such it cannot be depended upon in all browsers. If you were to use a valid id, it would then be valid.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    err, no the div id is "star" (the text not *) and i want the script to find the symbol * on the div and replace the * with an image?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Oh:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <div id="star">Hi * there!</div>
    <script type="text/javascript">
    var star=document.getElementById('star');
    star.innerHTML=star.innerHTML.replace(/\*/, '<img src="http://www.google.com/intl/en_ALL/images/logo.gif">');
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2006
    Posts
    74
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    cheers

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

    Default

    Or, validly:
    Code:
    function replaceStars(element) {
      var firstTextNode = element.firstChild,
        texts,
        img = document.createElement("img"),
        i;
      img.src = "star.png";
      while(firstTextNode.nodeType !== 3)
        firstTextNode = firstTextNode.nextSibling;
      texts = firstTextNode.nodeValue.split("*");
      for(i = 0; i < texts.length; ++i)
        if(i &#37; 2 === 0)
          texts[i] = document.createTextNode(texts[i]);
        else
          texts.splice(i, 0, img.copy());
      texts.reverse();
      element.replaceChild(firstTextNode, texts[0]);
      for(var i = 1; i < texts.length; ++i)
        element.insertBefore(texts[i], texts[i - 1]);
    }
    
    replaceStars(document.getElementById("star"));
    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!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Or, validly:
    How is that any more or less valid? I will say this though, it looks like a nightmare. No wonder you have trouble sleeping at times.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    How is that any more or less valid?
    innerHTML isn't part of any standard. The DOM, however, is.
    I will say this though, it looks like a nightmare. No wonder you have trouble sleeping at times.
    Hah, that's nothing.
    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!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    innerHTML isn't part of any standard. The DOM, however, is.Hah, that's nothing.
    Heh. Yeah, I keep forgetting that, perhaps because there is so little chance that it will ever stop being supported by mainstream browsers.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    Default

    It's already started... most browsers won't allow innerHTML in an XHTML document.

    There's little chance that broken HTML will stop being supported in mainstream browsers too. Should we all use it?
    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
  •