Today, I need to discuss the effect of return false
usage in an event handler if you are using jQuery.
I am providing a simple HTML content to check this out in real time. Here is our HTML content.
<!DOCTYPE html> <html lang=""> <body> <div class="link-holder" id="link-holder"> <a class="my-link" id="my-link" href="index.php">I am link</a> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/script.js"></script> </body> </html>
Nothing fancy here. Our html page contains a simple attribute tag enclosed by a div
container. We added jQuery from Google CDN and also a custom script file js/script.js
.
Default Working of Event Handlers
Our scipt.js file looks like this:
$(function ($) {
var $a = $('.my-link');
var $div = $('.link-holder');
$a.on('click', function () {
$(this).text('Hi From First');
});
$a.on('click', function () {
$(this).text('Hi From Second');
});
$div.on('click', function () {
$a.text('Hi From Third');
});
});
As you can see it registers three click event handlers. Two of them for attribute tag and one for the container div
tag.
When we execute and click on the attribute link, what would you think of the result? If you are thinking it would be Hi From Third
, you are wrong. The page will get reloaded and the text would be again I am link
.
The reason for this is simple. Even though it registers three event handlers for the click event, none of it stopping the default behaviour of attribute tag. So after executing all the event handlers, it will go on with its default behaviour which is loading the href
page mentioned.
The Effect: Stopping Default Behaviour
If you want to stop this, you can add event.preventDefault()
in any of the event handlers. This will stop the default behaviour attribute tag. To demonstrate this, we are changing the second event handler to below:
$a.on('click', function (event) {
event.preventDefault();
$(this).text('Hi From Second');
});
Now if you reload the page and click on the attribute tag, then you will see below result
As you can attribute tag default behavior is no more working and it shows the last executed event handlers output.
Now we are doing changing the code like this below:
$(function ($) {
var $a = $('.my-link');
var $div = $('.link-holder');
$a.on('click', function () {
$(this).text('Hi From First');
});
$a.on('click', function () {
$(this).text('Hi From Second');
});
$div.on('click', function () {
$a.text('Hi From Third');
return false;
});
});
Here the only difference is, we are adding return false
in the 3rd event handler. Now if you reload the page and click on the attribute tag, you will see the same output as you see above.
That’s cool. We now understood adding a return false
prevents the default behaviour, just like event.preventDefault()
does.
The effect: Stopping Event Propagation
Now let us put the same return false
statement in the second event handler as you can see below:
$(function ($) { var $a = $('.my-link'); var $div = $('.link-holder'); $a.on('click', function () { $(this).text('Hi From First'); }); $a.on('click', function () { $(this).text('Hi From Second'); return false; }); $div.on('click', function () { $a.text('Hi From Third'); }); });
The result is shown below:
Ah.. that’s interesting. It looks like the third event handler didn’t execute at all. so return false
stopping next event handlers from execution? It looks like so. But wait, Let us add the return false
in the first event handler in order to make sure it.
$(function ($) {
var $a = $('.my-link');
var $div = $('.link-holder');
$a.on('click', function () {
$(this).text('Hi From First');
return false;
});
$a.on('click', function () {
$(this).text('Hi From Second');
});
$div.on('click', function () {
$a.text('Hi From Third');
});
});
If you check the output now, you will see Hi From Second
. Exactly what you see above!!. This mean it executes all event handlers that are bind on attribute tag, but not the event handler that bind on the container div
.
Whenever an event is fired on the element, the browser will fire the same event for all of its parent elements. This is known as event propagation. So here return false
stops the event propagation. But, it will keep on executing event handlers bind on the same element though!!!
Effect of return;
or return true
If you try other return formats such as return;
or return true
, you will observe that there is no effect for them. For example, if you add any one of it in the first event handler and try out, you will see that page will reload again (default behaviour of attribute tag).
Is there any effect in pure javascript if use return false
There are no such “side effects” if we use pure javascript for it. For example , the below code:
$(function () {
var a = document.getElementById('my-link');
var div = document.getElementById('link-holder');
a.addEventListener('click', function (event) {
event.target.innerText = 'Hello From First';
return false;
});
a.addEventListener('click', function (event) {
event.target.innerText = 'Hello From Second';
});
div.addEventListener('click', function () {
a.innerText = 'Hello from Third';
});
});
You will see the output as the page reload again. So there is no effect for return false
in pure javascript.
промокод melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мел бет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на ставку мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет на первый депозит Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокод на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет на бесплатную ставку Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокод при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на ставку мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
купоны мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
бонус код мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод для мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокод на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод для melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет на бесплатную ставку Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод для melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокод на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мелбет на бесплатную ставку Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды на мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды на мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мел бет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
бонус код мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод для melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на мел бет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на бесплатную ставку melbet Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
melbet промокод Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокоды мелбет при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet при регистрации Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод в мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод melbet на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
бонус код мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод мелбет при регистрации на сегодня Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
промокод на ставку мелбет Click Here:👉 http://lynks.ru/geshi/php/?melbet_promokod_pri_registracii_2020.html
бесплатный видео чат с девушками. Click Here:👉 http://rt.livepornosexchat.com/
Code Promo 1xBet. Click Here:👉 https://popvalais.ch/wp-includes/inc/?code-promo-1xbet-burkina-faso-78-000xof.html
coursework vs research masters coursework college coursework sample coursework examination
coursework planner coursework advantages and disadvantages coursework vs dissertation coursework writing uk
coursework bibliography coursework ucl coursework based degrees coursework requirements
coursework moderation
coursework writer uk
coursework marking
courseware mit coursework b science titles coursework history coursework vs dissertation
quantitative coursework examples coursework marking java coursework nea coursework history a level
coursework uitm do my coursework for me coursework report coursework bank
coursework sample of written work course workload coursework program coursework report
coursework for bcaba coursework spelling creative
writing coursework ideas coursework resources
coursework based a levels coursework in a sentence
coursework resources courseworkdomau.com
coursework writing service uk coursework vs course work custom coursework writing service coursework buy uk
https://over-the-counter-drug.com/# over the counter viagra
Промокод 1xbet. Click Here:👉 https://www.medtronik.ru/images/pages/bonus_kod_na_1xbet_pri_registracii_6500_rubley.html
doxycycline order online cheap doxycycline online or doxycycline 100mg capsules
http://detroit.business/__media__/js/netsoltrademark.php?d=over-the-counter-drug.com buy doxycycline online uk
doxycycline prices doxycycline hyc and doxycycline tetracycline doxycycline tablets
order amoxicillin online no prescription amoxil
https://doxycycline.science/# buy generic doxycycline
cheap amoxil amoxicillin 500mg capsule cost
1xbet promo code. Click Here:👉 http://https://www.lafp.org/includes/pages/1xbet-promo-code-1xbet-bonus.html
amoxicillin 500 mg for sale generic amoxil online
https://stromectol.science/# stromectol 3 mg dosage
generic zithromax zithromax generic price
https://zithromax.science/# zithromax price south africa
doxycycline mono doxycycline 100mg online
https://doxycycline.science/# purchase doxycycline online
zithromax 500 mg buy zithromax
https://zithromax.science/# where can i buy zithromax uk
amoxicillin pills 500 mg amoxicillin generic
stromectol xr minocycline 50mg without doctor or minocycline 100 mg tabs
http://theprandmarketingagency.com/__media__/js/netsoltrademark.php?d=stromectol.science ivermectin 9 mg
ivermectin brand minocycline 50 mg pills and ivermectin oral solution ivermectin oral solution
where can i get doxycycline buy cheap doxycycline online
https://amoxil.science/# generic amoxicillin online
doxycycline prices buy cheap doxycycline
https://stromectol.science/# buy ivermectin uk
zithromax cost of generic zithromax
can you buy amoxicillin over the counter rexall pharmacy amoxicillin 500mg or amoxicillin capsule 500mg price
http://mobirn.com/__media__/js/netsoltrademark.php?d=amoxil.science buy amoxicillin 500mg usa
canadian pharmacy amoxicillin can you buy amoxicillin over the counter and price of amoxicillin without insurance can you buy amoxicillin over the counter in canada
doxycycline medication doxycycline 100mg tablets or doxycycline online
http://videoserve.biz/__media__/js/netsoltrademark.php?d=doxycycline.science buy doxycycline online without prescription
price of doxycycline doxycycline 100mg price and buy cheap doxycycline doxycycline generic
https://doxycycline.science/# buy cheap doxycycline
amoxicillin 500 mg where to buy order amoxil
https://doxycycline.science/# buy doxycycline online uk
doxycycline hyc 100mg doxycycline without a prescription
order doxycycline 100mg without prescription doxycycline tablets
https://stromectol.science/# minocycline 100mg otc
cheap amoxil buy cheap amoxicillin
zithromax cost canada zithromax over the counter uk or zithromax buy
http://ww2.elfbeautynetwork.com/__media__/js/netsoltrademark.php?d=zithromax.science zithromax 500 mg lowest price drugstore online
buy zithromax without prescription online zithromax online no prescription and zithromax online usa no prescription zithromax capsules 250mg
https://doxycycline.science/# doxycycline 100mg dogs
can i buy amoxicillin online can i buy amoxicillin online
doxycycline hyc 100mg where can i get doxycycline or where to purchase doxycycline
http://coastalrentalguide.com/__media__/js/netsoltrademark.php?d=doxycycline.science doxycycline hyclate
where can i get doxycycline vibramycin 100 mg and doxy doxycycline vibramycin
buy zithromax online with mastercard buy zithromax
https://zithromax.science/# can you buy zithromax over the counter in mexico
cheap amoxil cheap amoxicillin 500mg
ivermectin oral solution price of ivermectin liquid or ivermectin 1% cream generic
http://acdclyrics.com/__media__/js/netsoltrademark.php?d=stromectol.science what is minocycline prescribed for
buy stromectol ivermectin 6mg and stromectol buy uk п»їwhere to buy stromectol online
https://zithromax.science/# zithromax 250
cheap amoxil amoxicillin buy no prescription
can you buy amoxicillin over the counter canada buy amoxil
https://stromectol.science/# minocycline 50 mg online
zithromax for sale zithromax price south africa
https://doxycycline.science/# online doxycycline
Learn about the side effects, dosages, and interactions. safe and effective drugs are available.
https://stromectolst.com/# stromectol cost
Read here. Definitive journal of drugs and therapeutics.
Read information now. Definitive journal of drugs and therapeutics.
https://stromectolst.com/# ivermectin 200mg
Get information now. safe and effective drugs are available.
earch our drug database. Drug information.
stromectol medicine
Read information now. Drug information.
Get information now. Cautions.
https://stromectolst.com/# ivermectin price comparison
Everything information about medication. Commonly Used Drugs Charts.
ivermectin cream canada cost ivermectin 0.5 or minocycline drug
http://robinwolfson.net/__media__/js/netsoltrademark.php?d=stromectol.science minocycline 100mg over the counter
buy minocycline 50 mg for humans ivermectin for humans and buy minocycline 50 mg ivermectin 6 mg tablets
Read information now. earch our drug database.
stromectol price in india
Medscape Drugs & Diseases. Get warning information here.
Long-Term Effects. Medscape Drugs & Diseases.
https://stromectolst.com/# ivermectin price usa
Cautions. Drugs information sheet.
ivermectin nz can you buy stromectol over the counter or ivermectin 1mg
http://internationallogisticsincorporated.com/__media__/js/netsoltrademark.php?d=stromectolst.com ivermectin 18mg
ivermectin 80 mg ivermectin 4000 mcg and stromectol canada ivermectin cost canada
stromectol generic stromectol tablet 3 mg or buy stromectol online
http://zealspeculator.com/__media__/js/netsoltrademark.php?d=stromectolst.com stromectol tablets for humans
ivermectin humans ivermectin for sale and ivermectin 1 cream 45gm stromectol where to buy
buy ivermectin uk ivermectin 3mg pill or ivermectin 90 mg
http://www.bluecollaboration.biz/__media__/js/netsoltrademark.php?d=stromectolst.com ivermectin buy
buy stromectol online ivermectin 1 topical cream and cost of ivermectin ivermectin stromectol
Drugs information sheet. Some trends of drugs.
ivermectin 18mg
Everything information about medication. Everything about medicine.
minocycline 100mg tablets online stromectol for humans or stromectol over the counter
http://northavondale.com/__media__/js/netsoltrademark.php?d=stromectol.science stromectol 12mg online
ivermectin 4 order minocycline 100mg online and topical minocycline stromectol medicine
Read information now. drug information and news for professionals and consumers.
ivermectin 4 tablets price
Long-Term Effects. Drug information.
Drugs information sheet. Learn about the side effects, dosages, and interactions.
stromectol 6 mg tablet
Learn about the side effects, dosages, and interactions. Definitive journal of drugs and therapeutics.
Learn about the side effects, dosages, and interactions. Long-Term Effects.
ivermectin cost canada
Get information now. Read now.
Get warning information here. Read information now.
https://stromectolst.com/# ivermectin uk
What side effects can this medication cause? All trends of medicament.
п»їMedicament prescribing information. Get warning information here.
stromectol tablets for humans
Definitive journal of drugs and therapeutics. Cautions.
п»їMedicament prescribing information. Best and news about drug.
oral ivermectin cost
п»їMedicament prescribing information. Read now.
minocycline 100 mg pills online stromectol tablets uk or order minocycline 50 mg online
http://softcaps.biz/__media__/js/netsoltrademark.php?d=stromectol.science ivermectin 3mg
ivermectin australia minocycline for rheumatoid arthritis and ivermectin 50 stromectol price us
Comprehensive side effect and adverse reaction information. Generic Name.
https://stromectolst.com/# ivermectin
Long-Term Effects. Drug information.
ivermectin 6 mg tablets ivermectin 1 topical cream or ivermectin 9 mg
http://shallwetalk.net/__media__/js/netsoltrademark.php?d=stromectolst.com ivermectin 200mg
cost of ivermectin cream ivermectin pills human and cost of ivermectin ivermectin australia