views:

48

answers:

4

I have this hidden link, which is needed for other purposes:

<span id="notitle" style="display: none;">
<a href="...Foo" title="Foo" style="..."></a>
</span>

The link is generated dynamically and automatically includes the title attribute. But I'd like to remove the title attribute since the value is copied when the user copy-pastes surrounding text.

I thought of using javascript. This is what I've got so far:

<html>
    <head>
        <script type="text/javascript">
          function notitle() {
            var mylist=document.getElementById("notitle")
            var listitems= mylist.getElementsByTagName("a")
            for (i=0; i<listitems.length; i++) {
              listitems.setAttribute("title", "");
            }
      }
        </script>
    </head>

<body onLoad="notitle()">

<p>Before hidden link:
<span id="notitle" style="display: none;">
<a href="#Foo" title="Foo">This Link should have no title attribute</a>
</span>
After hidden link.</p>
</body>
</html>

But doesn't work. I guess it's about listitems.setAttribute("title", ""); Any idea? Cheers :)

+2  A: 

listitems is the collection, so your code is probably throwing an error.

Anyway, you want:

listitems[i].setAttribute("title", "");
Evan Trimboli
+1. You could also use [ `removeAttribute("title")` ](https://developer.mozilla.org/en/DOM/element.removeAttribute)
Andy E
that's it. thanks!
Martin
That's even better. Now I saw it only works for the first time, but not on following links like that. How would I apply that?
Martin
+1  A: 

First of all, you need to select the one specific item by using listitems[i], second, I think you can do it as simple as this:

listitems[i].title = ""
peirix
A: 

You need to add the i index:

listitems[i].setAttribute("title", "");
Mario Menger
that's it. thanks!
Martin
A: 

You're doing

listitems.setAttribute("title", ""); 

i times.

Add an array index into the list.

paque
omg, you guys are fast!
Martin