Aleksandr Petric Aleksandr Petric - 5 months ago 25
jQuery Question

Change link after click

i need some help with js.
I have few links.
After click on link, this link must change onclick="add('*');" on href="url.com"

<a onclick="add('1');"> Link1</a>
<a onclick="add('2');"> Link2</a>
<a onclick="add('3');"> Link3</a>
<a onclick="add('4');"> Link4</a>
<a onclick="add('5');"> Link5</a>


I will be very grateful for the help

Answer

This should get you started:

$('a').click(function(e){
  e.preventDefault(); //stop a from trying to navigate away
  $(this).attr('onclick', "add('*')");
  $(this).attr('href', 'http://google.com');
  
});

$('button').click(function(){
  /* Test the results*/
  $('a').each(function(){
    alert( $(this).attr('onclick') );
    alert( $(this).attr('href') );
  });
});

function add(){}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a onclick="add('1');"> Link1</a>
<a onclick="add('2');"> Link2</a>
<a onclick="add('3');"> Link3</a>
<a onclick="add('4');"> Link4</a>
<a onclick="add('5');"> Link5</a>

<button>Test the results</button>

Comments