469,338 Members | 8,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Changing anchor text

Hi guys,

I would like to change the text in an anchor.

es.

from
<a id="idanchor">oldtext</a>

to
<a id="idanchor">newtext</a>
I found that under Explorer, it is possible to do it as:
anchor = document.getElementById('idanchor');
anchor.innerText = "newtext";

Under Mozilla, such anchor property is called "text", but if I write:
anchor = document.getElementById('idanchor');
anchor.text = "newtext";

the Javascript console gives me the following error:
"setting a property that has only a getter"

it looks like the text property can only be read, but not written.
Do you know any other way to change the text anchor also under Mozilla?

Cheers

Daniele
Jul 23 '05 #1
6 27391
Daniele Baroncelli schrieb:
anchor = document.getElementById('idanchor');
anchor.innerText = "newtext";


anchor.innerHTML = "newtext";
HTML formating also possible: "new<b>text<\/b>"

Daniel
Jul 23 '05 #2
On Thu, 16 Dec 2004 13:05:57 +0100, Daniel Kirsch
<Iw*****************@gmx.de> wrote:

[snip]
anchor.innerHTML = "newtext";
Though I've found text-only replacement can be much quicker using a
DOM-only approach:

anchor.firstChild.data = 'new text';

or

anchor.replaceChild(document.createTextNode('new text'),
anchor.firstChild)
HTML formating also possible: "new<b>text<\/b>"


Presentational elements should[1] be avoided.

Mike
[1] Elements such as B and I are not deprecated (U is, but I think they
all should be), but their use is formally discouraged.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter schrieb:
Though I've found text-only replacement can be much quicker using a
DOM-only approach:

anchor.firstChild.data = 'new text';

or

anchor.replaceChild(document.createTextNode('new text'),
anchor.firstChild)


Yes. If you can be sure there is only one child element in your a
element. Which isn't the case if there is a comment and some text or
just a simple <br> with a space before and/or after. Speed migh be an
argument if you do *a lot* of replacements.

HTML formating also possible: "new<b>text<\/b>"


Presentational elements should[1] be avoided.


Of course. That should just show the flexibility of innerHTML which
isn't part of the w3c DOM but is implemented in all relevant browser.

Daniel
Jul 23 '05 #4
JRS: In article <opsi3qu5pwx13kvk@atlantis>, dated Thu, 16 Dec 2004
13:05:24, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
[1] Elements such as B and I are not deprecated (U is, but I think they
all should be), but their use is formally discouraged.


In some fields of work, standard conventions explicitly call for italics
to be used, and maybe bold also/instead.

While a non-GUI browser will have to do whatever it can, the appearance
of a GUI rendition of such material should not be determined by what
<em> or <strong> or whatever the browser has been set to produce. If
the user's CSS maps italic to purple, that's his problem.

See, for example, SUNAMCO 87-1 == IUPAP-25 (which had formal authority
over TB-L when the Web started!). URL not known.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk / ??*********@physics.org
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Jul 23 '05 #5
Daniel Kirsch wrote:
Michael Winter schrieb:
Though I've found text-only replacement can be much quicker using a
DOM-only approach:

anchor.firstChild.data = 'new text';

or

anchor.replaceChild(document.createTextNode('new text'),
anchor.firstChild)

Yes. If you can be sure there is only one child element in your a
element. Which isn't the case if there is a comment and some text or
just a simple <br> with a space before and/or after. Speed migh be an
argument if you do *a lot* of replacements.


The innerHTML also has that problem. If you replace its innerHTML, then
you wipe out what was there. You can append to the beginning or the end,
quite easily, with innerHMTL. You can also add in the middle with only a
trivial amount of extra work.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #6
On Thu, 16 Dec 2004 23:00:50 +0000, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <opsi3qu5pwx13kvk@atlantis>, dated Thu, 16 Dec 2004
13:05:24, seen in news:comp.lang.javascript, Michael Winter
<M.******@blueyonder.co.invalid> posted :
[1] Elements such as B and I are not deprecated (U is, but I think they
all should be), but their use is formally discouraged.


In some fields of work, standard conventions explicitly call for italics
to be used, and maybe bold also/instead.


So? A truly semantic document would mark-up what was inferred by that
presentation. Granted, it would be bulky with HTML (lots of SPAN elements
with classes), but then again HTML isn't really geared towards displaying
mathematical content.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Kenny | last post: by
8 posts views Thread by Margaret MacDonald | last post: by
5 posts views Thread by elsenraat_76 | last post: by
1 post views Thread by Nathan Sokalski | last post: by
6 posts views Thread by Richard Brown | last post: by
1 post views Thread by JB | last post: by
3 posts views Thread by Joseph Gruber | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.