Reputation: 1977
I need to iterate through all anchors on a page with specific hrefs and replace the href contents using jquery.
I need this:
<a onfocus="OnLink(this)" href='javascript:CoreInvoke("MtgNavigate","20131211");' target="_self"><u>12/11/2013</u></a>
to be replaced with this:
<a onfocus="OnLink(this)" href='javascript:MtgNavigate("20131211");' target="_self"><u>12/11/2013</u></a>
Basically, I want to replace CoreInvoke with MtgNavigate and remove the MtgNavigate as a parameter.
Each link though will have a different number in the second parameter of the original link. Any ideas how I can do this?
Upvotes: 1
Views: 298
Reputation: 8539
Check this fiddle.
$(document).ready(function(){
$("a").each(function(){
var token = $(this).attr("href");
token = token.substring(token.indexOf(',"')+2, token.indexOf('")'));
$(this).attr("href","javascript:MtgNavigate(\""+token+"\");");
});
});
The fiddle will execute on page ready. If you need to do this after any event means create a function and trigger on the event execution.
Upvotes: 2
Reputation: 10746
Using jquery it would go something like:
$('a').each(function(){
var href=$(this).attr('href');
if(href.indexOf('javascript:CoreInvoke')==0)
$(this).attr('href','javascript:' + href.substring(href.indexOf('("')+2, href.indexOf('","')) + '(' + href.substring(href.indexOf('","')+2));
});
Upvotes: 1