skip to Main Content

I want Change Other tag href url my code is

<a onclick=" change url with ( 'https://google.com') " > simple1 </a>
<a onclick=" change url with ( 'https://yahoo.com') " > simple2 </a>
<a onclick=" change url with ( 'https://msn.com') " > simple3 </a>




i want change this link

<a href="(( here must change ))" > changed url </a> 

3

Answers


  1. Use some basic javascript to do this:

    function changeUrl(url) {
      // get the a element
      var changedLink=document.getElementById('changedLink');
      // change the link
      changedLink.href=url;
    }
    <a onclick="changeUrl('https://google.com')">simple1</a>
    <a onclick="changeUrl('https://yahoo.com')">simple2</a>
    <a onclick="changeUrl('https://msn.com')">simple3</a>
    
    <a id="changedLink" href="initial-url">changed url</a>
    Login or Signup to reply.
  2. If I understand you correctly:

    window.addEventListener('DOMContentLoaded',() => {
      const tgtLink = document.getElementById('targetLink');
      document.getElementById('linkContainer').addEventListener('click',(e) => {
        const tgt = e.target.closest('a');
        if (!tgt) return; // not a link
        tgtLink.href = tgt.dataset.url;
      })
    });
    <div id="linkContainer">
    <a href="#" data-url="https://google.com">Simple1</a><br/>
    <a href="#" data-url="https://msn.com">Simple2</a><br/>
    <a href="#" data-url="https://yahoo.com">Simple3</a><br/>
    </div>
    
    <a href="#" id="targetLink">Changed</a>
    Login or Signup to reply.
  3. Use JS to do this.

       <a onclick="changeLink('https://google.com')">simple1</a>
      <a onclick="changeLink('https://yahoo.com')">simple2</a>
      <a href="(( here must change ))" id="targetLink">changed url</a>
    
      <script>
        function changeLink(newURL) {
          const targetLink = document.getElementById('targetLink');
          targetLink.href = newURL;
        }
      </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search