Stop redirect on clicking a link - javascript

Is there a way to stop the page from redirecting on clicking a link? I'm searching for a function like preventDefault().

event.preventDefault() actually is vanilla JavaScript and can achieve what you're looking for.
For example, with the following markup:
Google
We can attach the following JavaScript to prevent the browser from heading to http://www.google.com/ when the <a> element is clicked:
document.getElementById('preventme').addEventListener(
'click', function(e) { e.preventDefault(); }, false
);
jsFiddle Demo

You can add this little function in your click handler:
event.stopPropagation()

Attach an event handler function that will "return false"

Related

Why can't I trigger the default href on another anchor with JS?

I'm posting this because I can't find the same question elsewhere.
I'm trying to trigger the default action of an anchor but calling .click() or .trigger('click') in the click handler of another anchor.
Like so:
HTML:
<!-- I want to simulate a user clicking link2 when the user actually clicks link 1. -->
<!-- My guess is that triggering click just triggers any JS-bound click handlers. But that would defeat the point of e.preventDefault() when you usually have to call this to stop the default href being followed -->
<a id="link1" href="#">Click</a>
<a id="link2" target="_blank" href="http://google.com">Link 2</a>
JS:
$(document).ready(function(){
$('#link1').on('click', function(){
$('#link2').click();
$('#link2').trigger('click'); // Neither work
});
});
I feel like such a noob but nothing happens on click. Is this something that is blocked for security or accessibility?
I do not want to use window.open();
fiddle: http://jsfiddle.net/0hggdkzb/
try
jQuery(document).ready(function(){
$('#link1').on('click', function(){
// $('#link2').click().css('color','red');
document.getElementById("link2").click();
});
});
DEMO
Or
you can trigger event $('#link2')[0].click();
Triggering-event-handlers clears about this,
The .trigger() function cannot be used to mimic native browser events,
such as clicking on a file input box or an anchor tag. This is
because, there is no event handler attached using jQuery's event
system that corresponds to these events.
The jQuery UI Team created jquery.simulate.js in order to simplify
triggering a native browser event for use in their automated testing.
Its usage is modeled after jQuery's trigger.
$( "a" ).simulate( "click" );
And for your problem you have to use javascript core event as #Bala answered like,
$(function(){
$('#link1').click(function(){
$('#link2')[0].click();
});
});
or use location.href like,
$(function(){
$('#link1').click(function(){
window.location = $('#link2').attr('href');
});
});
Also, Hoffman gave the answer for the same issue
Try this:
$(document).ready(function()
{
$("#link1").click(function()
{
$('#link2')[0].click();
});
});

stopping javascript action on a link inside a div

I have a list of divs with onclick actions associated with them. Inside each div there is a normal link. When i click the link, the javascript is executed (which is fast) and THEN the new page associated with the link starts to load (a fraction of a second later).
What can i do to the link to make it not execute the javascript - although it is located inside the div defined in the script? Is there a way of excluding it from the association, or telling it to stop all javascript onclick?
Thanks!
you could stop the event propagation for the link inside your div elements, like so
$('div a').on('click', function(evt) {
evt.stopPropagation()
/* do something */
});
doing so, when you click on a link, the handler associated to the click event for div elements won't be executed.
Try this ,
$("#yourlinkId").click(function(event) {
event.preventDefault();
});
Have the onclick handler of the link return false, e.g. like this:
<a href="http://foo/bar" onclick="return false">
Or, in jQuery notation:
$('div a').on('click', function(e) { return false; });
or you could prevent the default action by
$('div a').on('click', function(event) {
event.preventDefault();
});
i think stop propogation would prevent any further events that are caused by clicking the link where as prevent default will just stop the actual link click?

It scrolls to the top of the page after clicking

I have this code to switch a switching button image:
$("#invio_scatola_on, #invio_scatola_off").click(function(){
$("#invio_scatola_off").toggle();
$("#invio_scatola_on").toggle();
});
when it is executed, the browser goes to the top of the page. why?
You probably need to prevent the default action of whatever element you click:
$("#invio_scatola_on, #invio_scatola_off").on('click', function(e){
e.preventDefault();
$("#invio_scatola_off, #invio_scatola_on").toggle();
});
Based on the few information that you've provided, if the browser is scrolling to the top, it means that you need to prevent its default behavior.
Check to see if the browser is appending "something" to the current URL...
You can prevent that like this:
$("#invio_scatola_on, #invio_scatola_off").click(function(event){
// prvent default behavior
event.preventDefault();
$("#invio_scatola_off").toggle();
$("#invio_scatola_on").toggle();
});
See the jQuery event.preventDefault() for more information on how it works.
I can only guess, because you didn't post your html code. Most probably your two elements are links with href="#". After you click handler the regular action is fired, navigating to the anchor # on your site (which is the top).
Try:
$("#invio_scatola_on, #invio_scatola_off").click(function(event){
$("#invio_scatola_off").toggle();
$("#invio_scatola_on").toggle();
event.preventDefault();
});
and see if that helps. More on event cancelling with jQuery here:
http://api.jquery.com/event.preventDefault/
I think you binded the click on anchor tags with href as # so for that you can use preventDefault() of javascript like :
$("#invio_scatola_on, #invio_scatola_off").click(function(evt){
evt.preventDefault();
$("#invio_scatola_off").toggle();
$("#invio_scatola_on").toggle();
});

does e.stopPropagation() in jquery works on anchor tag

i want prevent eventpropogation from element inside an anchor tag i m trying to use e.stopPropagation(); it doesn't seem to work is it possible or i m just wasting time please help me out of here
javascript is:
$(document).ready(function(){
$('.alink .p').click(function(e){
alert("hi");
e.stopPropagation();
});
html is :
<div>
<a href="http://google.com" class="alink" >Google links
<p class="p">Another Links to prevent default</p>
</a>
</div>
thanks for your precious time
event.stopPropagation() stops passing the event to handlers further away in DOM structure from the element, on which originally event was triggered. It does not, although, prevent action that has already been triggered.
You should use event.preventDefault() to stop mentioned above default action.
Sources:
event.preventDefault()
event.stopPropagation()
Description
It will not stop any default behaviours (such as link clicks) and you might want to consider using event.preventDefault() in addition to this method.
event.stopPropagation() is only for event handlers, not default behavior.
event.preventDefault() If this method is called, the default action of the event will not be triggered.
You had some spelling errors in your script and its document not "document".
Check out the Sample and this jsFiddle Demonstration
Sample
$(document).ready(function(){
$('.alink, .alink > .p').click(function(e){
e.preventDefault();
e.stopPropagation();
alert("hi");
});
});
More Information
jQuery - event.preventDefault()
jQuery - event.stopPropagation()
try this
$(document).ready(function(){
$('.alink .p').click(function(e){
e.preventDefault();
e.stopImmediatePropagation();
alert("hi");
return false;
});
});
you should use- e.preventDefault() to stop default behavior. stopPropagation is used to stop event bubbling.
You've missed enclosing brackets here, didn't you?
$('document').ready(function(){
$('.alink .p').click(function(e){
alert("hi");
e.stopPropagation();
});
Fix brackets and then use e.preventDefault() instead.

How to click the anchor using JQuery so that the href is also called?

I have an anchor and a text box as below:
<div id="container">
<input type="text" id="textBox" />
<a id="anchorTest" href="javascript: alert('inside anchorTest')">Click me</a>
</div>
I'm trying to make the default button to the anchor when the enter key is clicked:
$(document).ready(function () {
$('#anchorTest').click(function () {
alert('clicked');
});
$('#container').keyup(function (e) {
if (e.keyCode == 13) {
$('#anchorTest').triggerHandler('click');
}
});
});
But the anchor href javascript is never called?
How to click the anchor using JQuery so that the href is also called?
I used the below code and it worked but is there any better way as it submits the form whereas I'd like to have a pure javascript code:
window.location = $('#anchorTest').attr('href');
Thanks
jQuery handles the click event on anchor tags in a 'special' way. See this question for further information - Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?
From the question above -
I did some research and it seems that the .click is not suppose to
work with 'a' tags because the browser does not suport "fake clicking"
with javascript. I mean, you can't "click" an element with javascript.
With 'a' tags you can trigger its onClick event but the link won't
change colors (to the visited link color, the default is purple in
most browsers). So it wouldn't make sense to make the $().click event
work with 'a' tags since the act of going to the href attribute is not
a part of the onClick event, but hardcoded in the browser.
Change your "triggerHandler" to just "trigger" and see if that resolves your issue.
I think triggerHandler bypasses the standard behavior and executes the "onclick" event handler in your case which is not set.
if (e.keyCode == 13) {
$('#anchorTest').click();
}
Check from here http://jsfiddle.net/amBXV/

Categories

Resources