How to insert text between certain text I managed to do through <span> , but the problem is that <span> click <span> is visible on the clickable link.

 var P = document.querySelector('#wall'); P.onclick = function() { if (P.checked) { document.getElementById('wall1').innerHTML = 'wall,' ; } else { document.getElementById('wall1').innerHTML = '' ; } } 
 <input type="checkbox" id="wall">Отправка постов<br> <a href='https://oauth.vk.com/authorize?client_id=4616152&scope=<span id="wall1"></span>&redirect_uri=https://oauth.vk.com/blank.html&response_type=token'><b>ссылке</b></a> 

  • Need to paste text instead of <span id="wall1"></span> ? - Yuri

1 answer 1

This is not a working code. Make with replace :

 var P = document.querySelector('#wall'), a = document.querySelectorAll('a')[0]; P.onclick = function() { if (P.checked) { a.setAttribute('href', a.getAttribute('href').replace(/^(.*\&scope\=(?:[A-Za-z_0-9-]+\,?)*)[^&]*(\&.*)$/, '$1wall,$2')); } else { a.setAttribute('href', a.getAttribute('href').replace(/^(.*\&scope\=)[^&]*(\&.*)$/, '$1$2')); } console.log(a.getAttribute('href')); // Это что бы показать содержимое href } 
 <input type="checkbox" id="wall">Отправка постов<br> <a href='https://oauth.vk.com/authorize?client_id=4616152&scope=&redirect_uri=https://oauth.vk.com/blank.html&response_type=token'><b>ссылке</b></a> 

  • Thank you very much, but you can make the search not before &, so that the replace only searches for the scope, and after it inserts it, so that it does not search between the scope and &. To add a second method, for example audio - nicolaa
  • @nicolaa, in what sense? - Yuri
  • How to add a second method, for example audio, replace does a search between & scope = and &, when I add the second method, it replaces the first one - nicolaa
  • @nicolaa, I corrected the answer. Watch - Yuri