JavaScript Javascript replace linktext with title javascript replace link text,javascript change link text

I regularly visit a website with a lot of links of the following format:

<a target='_blank' href="SomeURL" title="SomeTitle" id="SomeID">SomeText</a>

Sadly the part SomeText is useless in most cases and I want to replace it with SomeTitle. Therefor I tried my luck with a Greasemonkey-Script (inject Javascript to a webpage). I want to modify all Links on the page.

My goal is this:

<a target='_blank' href="SomeURL" title="SomeTitle" id="SomeID">SomeTitle</a>

I already tried my luck, but since I'm not familiar with javascript this is all I got so far, of course it did not work...

document.links.innerHTML = document.links.title

I hope somebody can help me with this problem. Thanks in advance.

Edit: Thanks for the fast replies, I add a comment to each answer with a feedback, if it solves the problem or not.

Its just strange, that none of these solution have a 100% success-rate. Sometimes I have to reload the page 2-3 times to get the code working. (Not a cache-problem, I swear) I though it maybe have to load the page fully to work, but when I add a timeout before running the code it only works on a few divs and not the entire page. This is just additional information, no further help is needed. Im fine, if I have to reload the page sometimes.

Answer:1

After the document loads:

var theLink = document.getElementById('SomeID');
theLink.innerHTML = theLink.getAttribute('title');

Or in your case all links:

var elements = document.getElementsByTagName('a');
[].forEach.call(elements, function(element) {
  element.innerHTML = element.getAttribute('title') || element.innerHTML;
});
Answer:2

Here's my solution.

var links = document.getElementsByTagName('a');  // get all the links.
for (var i = 0; i < links.length; i++) {
    var link = links[i];
    // It will replace text only if `title` is present.
    link.innerText = link.getAttribute('title') || link.innerText;
}
Answer:3

That should do the trick:

var i, links = document.getElementsByTagName('a');
for(i in links) { 
    var title = links[i].title;
    if(title) {
        links[i].innerHTML = title;
    }   
}
Answer:4

Currently in Ramda if I want to deep merge (right) multiple objects I.... var a = _.mergeDeepRight( { one: 1 }, { two: { three: 3 } } ) var b = _.mergeDeepRight( a, { three: { four: 4 } } ) var c = _....

Currently in Ramda if I want to deep merge (right) multiple objects I.... var a = _.mergeDeepRight( { one: 1 }, { two: { three: 3 } } ) var b = _.mergeDeepRight( a, { three: { four: 4 } } ) var c = _....

Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example: mergeEveryWayPossible([0,0,0],...

Example given in JavaScript: Suppose we have two arrays [0,0,0] and [1,1,1]. What's the algorithm to produce all possible ways these two arrays can be combine. Example: mergeEveryWayPossible([0,0,0],...

  1. algorithm combine arrays
  2. merge algorithm array
  3. algorithm merge sorted arrays
  4. algorithm merge two arrays
  5. algorithm merge two sorted arrays
  6. algorithm to merge two arrays in c

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?   function testrecur(s) { console.log("begin testrecur=" + s); s++...

Is this recursion coded wrong or is it just that console.log() is not always executed even if the recursion is executed?   function testrecur(s) { console.log("begin testrecur=" + s); s++...

  1. trigger dataflow from cloud function
  2. send email from cloud function
  3. query firestore from cloud function
  4. trigger dataflow from cloud function python
  5. run dataflow from cloud function
  6. get response from cloud function
  7. write to firestore from cloud function
  8. firebase send notification from cloud function
  9. call cloud function from cloud function

Currently screensharing on localhost in firefox throws the following error: The request is not allowed by the user agent or the platform in the current context.. Here's my code: navigator....

Currently screensharing on localhost in firefox throws the following error: The request is not allowed by the user agent or the platform in the current context.. Here's my code: navigator....