I have a HTML page where there are many <a onclick="javascript>
What i need to do using Jquery is find the below tag as it is
<a onclick="javascript:OpenNewWindow('/help_options.php?ID=2', 350, 250);" href="javascript:void(0);">
and replace it with
<a onclick="javascript:OpenNewWindow('/help_options.php?ID=2', 600, 500);" href="javascript:void(0);">
Note
1) the values 350 is changes to 600 and 250 is changed to 500 2) there are also many similar tags but i want the code to do Exact find and replace of the above tag.
IS this possible ?
2 Answers 2
I wouldn't be looking to change the inline onclick Javascript even if I could.
A better approach is to late-bind everything in JavaScript:
$(document).ready(function(){
$("#MyAnchorID1").click(function(){
OpenNewWindow('/help_options.php?ID=2', 350, 250);
});
});
So, when you did want to change the event, you unbind then re-bind. I.e. do something like:
$("#MyAnchorID1").unbind("click").click(function(){
OpenNewWindow('/help_options.php?ID=2', 600, 250);
});
Of course, that approach just does it for an anchor with a particular id. You could just select 'a' to get all anchors.
A simpler solution might be to define an object literal:
var myDims = {width:350, height:250};
Use that in the function:
OpenNewWindow('/help_options.php?ID=2', mydims.width, myDims.height);
And just change the values of this when you need:
myDims.width = 600;
3 Comments
$(document).ready(function () {
$('a').unbind();
$('a').click(function(){
OpenNewWindow('/help_options.php?ID=2', 600, 500);
});
});
javascript:label is superfluous in theonclickhandler. You should remove it.