Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
2 like 0 dislike
329 views
in Web Development by 20 36 42
edited by

In HTML, I have a hyperlink tag without any calss or id and I want to change its innertext for this A tag for example, from "test" to "exam". How to modify the innertext of 'a' tag in JavaScript?

<div class="list">
<span>
   <a href="https://test.com/">Test</a>
</span>
</div>

 


1 Answer

2 like 0 dislike
by 20 36 42
edited by

1) Replace the anchor tag innerText using JQuery

You can simple change the innerText using JQuery by the script below:

<!-- reference jQuery link -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 

<script type="text/javascript">

$('.list').find('a:contains("test")').text('exam');

</script>

2) Replace the anchor tag innerText Using JavaScript

Besides the baove method, you can also use the below JS script to chnage the Hyperlink Inner Text as below.

<script type="text/javascript">

const divList = document.getElementsByClassName('list');

for (let i = 0; i < divList.length; i++) {
const anchor = divList[i].getElementsByTagName("a");
    for (var y = 0; y < anchor.length; y++){
        if(anchor[y].innerText.includes('test')){
          a[y].innerText = 'exam';
         }
     }
}
</script>

See Also

If you don’t ask, the answer is always NO!
...