Hi all

I need a javascript code that removes all the title="*" attributes in a page, where the star (*) represents a wildcard for the content of the title attribute. So the following would occur:

<a href="#" title="test">test</a>
would be replaced with
<a href="#">test</a>

just like:

<a href="#" title="">test</a>
would be replaced with:
<a href="#">Test</a>

I know there is a function in javascript called match, but i have no idea how to write the reg expression that would work like the above example.

thanks in advance!!

Dani AI

Generated

Good catch — the problem was the code running before the DOM existed. ’s window.onload approach fixes the timing, but here’s a cleaner, more selective and more robust way that only touches elements that actually have a title attribute and runs as soon as the DOM is ready.

document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('[title]').forEach(function (el) {
    el.removeAttribute('title');
  });
});

Why this is better: querySelectorAll('[title]') selects only elements that have a title, so you avoid looping every node. DOMContentLoaded fires as soon as the document structure is ready (you don’t need to wait for images like window.onload). The snippet is short, fast, and easy to read.

If you need to support very old browsers that lack querySelectorAll or NodeList.forEach, use a tiny fallback that collects only elements with title and then removes it:

document.addEventListener('DOMContentLoaded', function () {
  var els = document.querySelectorAll ? document.querySelectorAll('[title]') : (function(){
    var a = document.getElementsByTagName('*'), r = [];
    for (var i = 0; i < a.length; i++) if (a[i].getAttribute('title')) r.push(a[i]);
    return r;
  })();
  for (var j = 0; j < els.length; j++) els[j].removeAttribute('title');
});

Note and caution: removing title can affect usability — some sites rely on it for tooltips or extra hints and some screen readers may use it. If you only want to drop empty titles or titles matching a pattern, check the attribute first and remove conditionally, or consider replacing the visible hint with aria-label for accessibility.

Recommended Answers

All 3 Replies

iv just found a function called removeattribute and am trying to figure out how to apply this to every element on the page.

So far I have this:

for(var i=0; document.all < document.all.length; i++) {
    document.all[i].removeAttribute("title");
}

but its not making any changes to the page.
Can anybody shed some light?

Hi there,

I think the script you wrote is being executed before any of the elements are actually created. Perhaps modifying it like this will have a better effect.

window.onload = function(){
	for(var i=0;i<document.all.length;i++) {
		document.all[i].removeAttribute("title");
	}
}

I replaced the document.all with i by the way.

Hope this helps,

Traevel

ahh great, it works now. thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.