I've got a javascript snippet that displays a widget on my page. There's some links that get output by the script that look like this:
<a href="#" onclick="somefunction()">Link</a>
These links cause some JS to fire. That's great. The problem is the href="#"
and the absence of a "return false;"
at the end of the onclick
attribute.
When I click one of those links, the browser jumps to the top of the document. With the widget positioned close to the bottom of the document, this is no good.
Unfortunately, I have no control over the output of the script.
Using jQuery I can reference these links using $("#wxheader ul li a")
. I tried the following code but it doesn't work:
$(document).ready(function(){
$("#wxheader ul li a").each(function(){
var onclick = $(this).attr("onclick") + "; return false;";
$(this).attr("onclick", onclick );
});
});
I want to write a jQuery function that will change each onclick attribute to append "return false;"
and it has to run after the script has output the content to the page.
Any ideas?
I've got a javascript snippet that displays a widget on my page. There's some links that get output by the script that look like this:
<a href="#" onclick="somefunction()">Link</a>
These links cause some JS to fire. That's great. The problem is the href="#"
and the absence of a "return false;"
at the end of the onclick
attribute.
When I click one of those links, the browser jumps to the top of the document. With the widget positioned close to the bottom of the document, this is no good.
Unfortunately, I have no control over the output of the script.
Using jQuery I can reference these links using $("#wxheader ul li a")
. I tried the following code but it doesn't work:
$(document).ready(function(){
$("#wxheader ul li a").each(function(){
var onclick = $(this).attr("onclick") + "; return false;";
$(this).attr("onclick", onclick );
});
});
I want to write a jQuery function that will change each onclick attribute to append "return false;"
and it has to run after the script has output the content to the page.
Any ideas?
Share Improve this question edited Feb 25, 2017 at 17:59 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Dec 7, 2010 at 20:10 WNRosenbergWNRosenberg 1,9025 gold badges23 silver badges31 bronze badges 06 Answers
Reset to default 4Try this. The trick is to call preventDefault
in the handler, which prevents the default element action from propagating. I hope this helps.
$("#wxheader ul li a").click(function(e){
e.preventDefault();
return false;
});
Have you tried using href="javascript:"
?
I'd do it like this
$( '#wxheader ul li a' ).each( function( i, element )
{
// Capture the existing callback function
var originalCallback = element.onclick;
// Now, remove it from the elemnet
element.onclick = null;
// And replace it with our own, which calls the orignal
// with the proper context, and prevents the default
// event action
$(element).click( function( event )
{
event.preventDefault();
originalCallback.call( window );
});
});
You should be able to override it in jquery, try this:
$("#wxheader ul li a").each(function(){
$(this).click(function(e) {
e.preventDefault();
});
});
This stops the normal process of the click event.
Fixed, this will effectively stop the browser's default interpretation of the click event
You could also use:
<a href="javascript:void(0)" onclick="somefunction()">Link</a>
Look in to the preventDefault object in jQuery.
http://api.jquery./event.preventDefault/
That will allow it to not run initially or also adding the return false in the jQuery click handler. preventDefault might not work in Firefox. I'm not entirely sure.
$("#wxheader ul li a").click(function(){
//do stuff//
return false;
});
This is a test page i made with some ajax functionality on a link for someone trying to override the regular click. this is the functionality I'm talking about it if its what you are looking for.
http://testing.kobbyappiah./Design_and_Evolve/ajaxTest.html