-4

I have below html:

<span class="myclass"><a href='javascript:$s("P3_MGR","7839","KING");'>KING</a>, <a href='javascript:$s("P3_MGR","7902","FORD");'>FORD</a>, <a href='javascript:$s("P3_MGR","7566","JONES");'>JONES</a></span>

I would like to know how can I replace comma (,) with semi colon(;) that's appearing after closing anchor tag

Thanks, Richa

Richa
  • 337
  • 4
  • 18
  • 1
    What does `$s()` do? – Barmar Nov 01 '17 at 21:20
  • Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – coagmano Nov 01 '17 at 21:25
  • sorry but how would I know its duplication before posting question? I did search before posting but no results were similar to what I want. – Richa Nov 01 '17 at 22:43
  • why negative vote for question? – Richa Nov 03 '17 at 01:26

2 Answers2

0

The simplest working, but ugly way:

document.querySelector('.myclass').innerHTML =  document.querySelector('.myclass').innerHTML.replace(/<\/a>,/g, '</a>;');

Nicer way, though still ugly:

var toReplace = document.querySelector('.myclass');
toReplace.innerHTML = toReplace.innerHTML.replace(/<\/a>,/g, '</a>;');
0

The right way with Document.querySelector() method and String.replace() function:

var span = document.querySelector('span.myclass');
span.textContent = span.textContent.replace(/,/g, ';');
<span class="myclass"><a href='javascript:$s("P3_MGR","7839","KING");'>KING</a>, <a href='javascript:$s("P3_MGR","7902","FORD");'>FORD</a>, <a href='javascript:$s("P3_MGR","7566","JONES");'>JONES</a></span>
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105