karadayi
karadayi

Reputation: 2269

Javascript clone text from anchor and add to attribute of same anchor

Trying to clone the text from anchor and set it as title attribute to same anchor but do not get the logic how to use the syntax correct.

$(document).ready(function () {
	$('.desc a').clone(true,true).appendTo($('.desc a').attr('title', ''));
});
<div class="desc">
  <a href="somedomain">Somedomain</a>
  <a href="somedomain2">Somedomain2</a>
</div>

Would be happy for a small help.

Upvotes: 0

Views: 31

Answers (1)

ibrahim tanyalcin
ibrahim tanyalcin

Reputation: 6491

I'm not exactly sure if I understand correctly, but if you want to get the text and set it back to the same anchor you do not need to clone the a tag:

function setTitle(node){
    node.setAttribute("title",node.textContent)
    return node;
}

[].slice.call(document.querySelectorAll('.desc a')).forEach((d)=>setTitle(d));

Upvotes: 1

Related Questions