Hello! There is such a link:

<a href="#" onclick="javascript:add('value1', 'opt1', 'value2', 'opt2', 'value3', 'value4'); return:false;">+</a> 

It is necessary to change only opt1 and opt2 for the event, all other values ​​are constant. Such links can be any number. Initially, the link is generated in php with predefined parameters and looks like this:

  <a href="#" onclick="javascript:add('555', '25', 'http://url', '280', 'text', 'name'); return:false;">+</a> 

I tried this:

 $('.target').change(function() { $(this).next('a').attr("onclick", "javascript:add('" +opt1+ "', '25', 'http://url', '"+ opt2 + "', 'text', 'name'); return:false;") }); 

But this code will work for only one link. others have distinct permanent attributes. I suppose that you first need to get attr ("onclick") and break the parameters into constants and those that will change.

  • Can't change the code to PHP? - Mihanik71

1 answer 1

Regulars are needed. Corrected version:

 $('a').each(function(id, el) { var $el = $(el); var onClickSrc = $el.attr('onclick'); var param1 = '"par1"'; var param2 = '"par2"'; var params = onClickSrc.match(/'[^']+'/g); params[1] = param1; params[3] = param2; onClickSrc = onClickSrc.replace( /\([^\)]+\)/, '(' + params.join(', ') + ')' ); $el.attr('onclick', onClickSrc); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" onclick="javascript:add('value1', 'opt1', 'value2', 'opt2', 'value3', 'value4'); return:false;">+</a> <a href="#" onclick="javascript:add('value1', 'opt1', 'value2', 'opt2', 'value31', 'value41'); return:false;">+</a> 

  • Thank you, but your code changes the first two parameters of 'value1', 'opt1', but you need opt1 and opt2 - vital mar
  • Fixed the decision. - Aleksander K.
  • Understood the direction :), and if we additionally consider the option when there are parameters without quotes in onclick, for example: document.getElementById ('element'). Value - vital mar