This also won't work if the link contains an inner <span> element.
–
TriptychDec 27 '08 at 18:51

Yeah, and if the question wasn't "what’s a better way [to] change the text of a link", you might have had a point...
–
ChristophDec 28 '08 at 19:30

As a generalised version if there might be other content in the a element, clear the content and add a new text node. eg.: while (link.firstChild) link.removeChild(link.firstChild); link.appendChild(document.createTextNode('New text'));
–
bobinceDec 29 '08 at 0:44

innerHTML has side effects (like disconnecting existing DOM nodes and rerendering that might be heavy). One needs to be aware of these effects. And anyone maintaining the code will need to be alert to that innerHTML is used or they might run into strange bugs.

"standard addict"? =) Standards have their uses though. Like encouraging a common implementation. innerHTML is there in most browsers but it works differently. Use it with care, I would say.
–
PEZDec 27 '08 at 16:56

<layer>s were once a "practical standard" too.
–
TriptychDec 27 '08 at 18:51

<bite>me</bite>! @PEZ All addicts reason for what they are addicted to as something useful. @Trip...whatever Isn't that toy specific to just netscape?
–
kavoir.comJan 6 '09 at 4:57

Up until a year ago, innerHTML was just a lot faster than manipulating events via the DOM. I haven't checked the latest versions of all major browsers for this myself.

Firefox for example doesn't handle this well. It sometimes only updates the screen to reflect the change. If you query the DOM after the change, it still has the old values.

Example: try to change the value of a textarea via innerHTML, and then post the form. It'll silently post the value that the textarea had before. Think of the catastrophic results that something like that could have.

@Wouter: Textarea is an input field and its value should be changed with element.value
–
someDec 27 '08 at 15:14

I know you can also change it like that. If you use innerHTML it is silently accepted, but it doesn't work out the way you expect it to.
–
Wouter van NifterickDec 27 '08 at 15:34

"silently accepted" - what would you rather have happen?
–
Greg DeanDec 27 '08 at 16:45

"As there is no public specification for this property, implementations differ widely. For example, when text is entered into a text input, IE will change the value attribute of the input's innerHTML property but Gecko browsers do not." (cont..)
–
someDec 27 '08 at 16:46