Changing anchor text

Discussion in 'Javascript' started by Daniele Baroncelli, Dec 16, 2004.

  1. 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
     
    Daniele Baroncelli, Dec 16, 2004
    #1
    1. Advertising

  2. Daniele Baroncelli schrieb:
    > anchor = document.getElementById('idanchor');
    > anchor.innerText = "newtext";


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

    Daniel
     
    Daniel Kirsch, Dec 16, 2004
    #2
    1. Advertising

  3. On Thu, 16 Dec 2004 13:05:57 +0100, Daniel Kirsch
    <> 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.
     
    Michael Winter, Dec 16, 2004
    #3
  4. 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
     
    Daniel Kirsch, Dec 16, 2004
    #4
  5. 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 / ?? ©
    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)
     
    Dr John Stockton, Dec 16, 2004
    #5
  6. Daniele Baroncelli

    Randy Webb Guest

    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
     
    Randy Webb, Dec 16, 2004
    #6
  7. On Thu, 16 Dec 2004 23:00:50 +0000, Dr John Stockton
    <> wrote:

    > JRS: In article <opsi3qu5pwx13kvk@atlantis>, dated Thu, 16 Dec 2004
    > 13:05:24, seen in news:comp.lang.javascript, Michael Winter
    > <> 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.
     
    Michael Winter, Dec 17, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Richter
    Replies:
    1
    Views:
    568
    Brock Allen
    May 20, 2005
  2. Replies:
    6
    Views:
    29,690
    Alan J. Flavell
    Apr 23, 2006
  3. irma
    Replies:
    0
    Views:
    123
  4. ehm
    Replies:
    3
    Views:
    211
    Richard Cornford
    Feb 13, 2004
  5. Christopher Nelson
    Replies:
    17
    Views:
    292
    Christopher Nelson
    Oct 31, 2005
Loading...

Share This Page