Click to See Complete Forum and Search --> : Replacing text in a browser window
mfinley
11-24-2002, 03:18 PM
Is it possible to update existing text in a browser window using Javascript?
I can change an image, but can't find way of replacing bits of text. (At present the text is in a cell of a table, but could be wrapped with <DIV> or <SPAN> if necessary (or maybe something else))
Similarly, can I update the background image of a table?
Thanks for any help,
mike
Rick Bull
11-24-2002, 04:50 PM
Some browsers support innerHTML:
<div id="divID">Here's some text</div>
<a href="#" onclick="if (document.getElementById) document.getElementById('divID').innerHTML = 'And here\'s some different text!'; return false;">Change Text</a>
And to change the background of an element same theory:
<div id="divID" style="background:url('image.png');">Here's some text</div>
<a href="#" onclick="if (document.getElementById) document.getElementById('divID').style.background = 'url(\'new_image.png\')'; return false;">Change Background</a>
EDIT: Whoops, the forum removed the backshashes - there should have been one before the single quote in here's in this phrase: "'And here's some different text!'" and also for both quotes inside the url() bit in the second example :rolleyes:
mfinley
11-25-2002, 03:38 AM
Thanks!
I'll try those out.
mike
mfinley
11-26-2002, 03:30 AM
Thanks, these worked.
Adding a DIV inside a table cell didn't however; it messed up the formattingand placed the text outside the cell. Fortunately your code also works placing the ID attribute directly in the TD tag.
mike
Rick Bull
11-29-2002, 04:27 AM
Sorry? I don't understand what you mean? :o
I did figure out on another post to add a backslash before quotes though :p Stupid forums!
Rick Bull
11-30-2002, 06:10 AM
Ah I see, thanks. As I said I actually figured that out after I posted. Bit of a stupid bug though.