0

I'm trying to change the text in the anchors as follows:

for (var i = 0; i < anchors.length; i++) { 
  ...
  anchors[i].text = anchors[i].text + "+";
}

I tried alternative methods like concat and append but nothing works.

anchors[i].innerHTML helped but the code is called on DOMContentLoaded and I want to add only "+" string, instead of it I'm adding unpredictable sequence of "+"* e.g. "++++++++"

thank you for help

xralf
  • 3,312
  • 45
  • 129
  • 200

3 Answers3

3

Simply put, HTMLElement.innerHTML is for HTML strings. In your case, you're looking for Node.textContent and the non-standard Node.innerText since you're just replacing text.

var anchors = document.links;

for(var i=0;i<anchors.length;i++)
{
    var anchor = anchors[i];
    if(anchor.textContent)
    {
        //W3C DOM
        anchor.textContent += "+";   
    }else if(anchor.innerText)
    {
        //Microsoft DOM
        anchor.innerText += "+";   
    }
}

When dealing with older browsers, you can use a snippet like the one bobince provided here (do note that it will clear the childNodes clean and replace them with a text node): 'innerText' works in IE, but not in Firefox

Reference:

  1. MDC: https://developer.mozilla.org/En/DOM/Node.textContent
  2. MSDN: http://msdn.microsoft.com/en-us/library/ms533899%28v=VS.85%29.aspx
  3. W3C DOM Level 3: http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent
Community
  • 1
  • 1
  • The problem with multiple "+" still remains. – xralf Jun 06 '11 at 19:39
  • 2
    Then your problem is one with event listeners (possibly bubbling). Try removing the event listener after the code executes. –  Jun 06 '11 at 19:45
  • I've tested it and it just adds one '+'. Are you running the code multiple times or are these links that already ended with '+' or does your `anchors` array contain more than one reference to each link? In any of those cases you would have to put a conditional statement in to make sure the string doesn't end with '+' before you add one. – Useless Code Jun 06 '11 at 19:55
  • @Matt McDonald I added removeEventListener and thought it helped but it not. But it speed up the code. – xralf Jun 06 '11 at 20:14
  • @Useless Code : The code runs on DOMContentLoaded event. Look [here](http://stackoverflow.com/questions/6256653/javascript-code-too-slow-in-firefox-extension-using-storage-service/) – xralf Jun 06 '11 at 20:16
  • @xralf Yeah, I noticed that after I posted that comment and read Matt McDonalds comment. – Useless Code Jun 06 '11 at 20:24
  • Have you tried removing the listener **before** the loop? My guess is that every time a new '+' is inserted it is triggering another DOMContentLoaded event, so you would need to remove the event before you start injecting things into the DOM. – Useless Code Jun 06 '11 at 20:34
  • The problem was caused by other firefox extensions that probably generate page loads (I can't see) and about:blank page. – xralf Jun 07 '11 at 11:12
1

JavaScript DOM does not have .text. It has .innerHTML

epascarello
  • 204,599
  • 20
  • 195
  • 236
1

If you mean the link text:

 var anchors = document.getElementsByTagName("A");
 for (var i = 0; i < anchors.length; i++) {
    anchors[i].innerHTML += "+";
 }
Alex K.
  • 171,639
  • 30
  • 264
  • 288