Changing anchor text

D

Daniele Baroncelli

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
 
D

Daniel Kirsch

Daniele said:
anchor = document.getElementById('idanchor');
anchor.innerText = "newtext";

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

Daniel
 
M

Michael Winter

On Thu, 16 Dec 2004 13:05:57 +0100, Daniel Kirsch

[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.
 
D

Daniel Kirsch

Michael said:
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
 
D

Dr John Stockton

JRS: In article <opsi3qu5pwx13kvk@atlantis>, dated Thu, 16 Dec 2004
13:05:24, seen in 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.
 
R

Randy Webb

Daniel said:
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.
 
M

Michael Winter

JRS: In article <opsi3qu5pwx13kvk@atlantis>, dated Thu, 16 Dec 2004
13:05:24, seen in Michael Winter
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top