how would i use the get element by id "star" and make it find a * and replace the * with an image?
Help appreciated![]()
how would i use the get element by id "star" and make it find a * and replace the * with an image?
Help appreciated![]()
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:
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.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>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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?
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
cheers![]()
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 % 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!
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
innerHTML isn't part of any standard. The DOM, however, is.How is that any more or less valid?Hah, that's nothing.I will say this though, it looks like a nightmare. No wonder you have trouble sleeping at times.![]()
![]()
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!
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
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