Reputation: 41
i have the below anchor tag in my jsp
<a href="javascript:submit(this)">save</a>
<a href="javascript:alert(this)">something</a>
I require to stop the default, href behaviour and yet have these functions called at onclick. For the same i have written this piece of code:
$('a[href^=\'#\']').live("click", function (e) {
alert("I am being called #");
e.preventDefault();
return false;
});
$(document).ready(function(){
$("a[href^='javascript']").each(function(){
alert("replacing");
var w=$(this).attr('href');
$(this).attr('href','#');
$(this).attr("onclick",w);
});
});
However, the functions are not being called on clicking the links. Could you please tell me where I am going wrong. I am using IE 8.
Upvotes: 2
Views: 3012
Reputation: 15
$(document).ready(function() {
var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href', "#");
aTag.setAttribute('id', "tagId");
aTag.innerHTML = "link text";
mydiv.appendChild(aTag);
$("#tagId").on('click', function(e) {
alert(1)
});
});
Upvotes: -1
Reputation: 1663
Try this:
$(document).on("click", "a[href^=\'#\']",function (e) {
alert("I am being called #");
e.preventDefault();
return false;
});
$(document).ready(function(){
$("a[href^='javascript']").attr('href','#');
});
Upvotes: 3