Logo

dev-resources.site

for different kinds of informations.

Common JavaScript "event Handler" Mistake

Published at
6/25/2024
Categories
javascript
jquery
programming
coding
Author
sagar7170
Author
9 person written this
sagar7170
open
Common JavaScript "event Handler" Mistake

When i was dealing javacript click event i was doing this mistake see the blow javascript & jquery code snippet

javascript code example

let name = 'herry';

document.getElementbByClassName('demo').addEventListener('click',function(){
  name = "joy";
})

console.log(name) // herry
Enter fullscreen mode Exit fullscreen mode

jquery code example

let name = 'herry';

$('.demo').on('click',function(){
  name = "joy";
})

console.log(name) // herry
Enter fullscreen mode Exit fullscreen mode

Both javascript & jquery code are same

Mistake i was doing

I was trying to update and access it β€˜name’ variable after click event triggered as you can see in the above code
when console.log(name) outside the click variable name printed as herry but it was supposed to be as joy

Why name Doesn't Update Immediately

The behavior of the click event is asynchronous due to this behavior the code lines executed as

  1. let name = β€˜herry’;
  2. console.log(name);
  3. $(β€˜.demo’).on(β€˜click’,function(){ name = β€œjoy”; })

Order of Execution: JavaScript executes the code sequentially from top to bottom. When console.log(a); is executed, the click event hasn't happened yet, so name is still 'herry'.
Asynchronous Event Handling: The click event handler is an asynchronous operation. It only runs when the user clicks on the element with class .demo. Until the click event happens, the code inside the event handler does not run.

If you want to see the updated value of a after the click event, you should log a inside the click handler:

let name = 'herry';

$('.demo').on('click',function(){
  name = "joy";
 console.log(name) // joy
})
Enter fullscreen mode Exit fullscreen mode

another way

let name = 'herry';

$('.demo').on('click',function(){
  name = "joy";
  UpdateValue(name);
})
function UpdateValue(name){
  console.log(name) // joy
}
Enter fullscreen mode Exit fullscreen mode
jquery Article's
30 articles in total
Favicon
Reading Progress Bar
Favicon
Add Row (Clone)
Favicon
Make PDF to Images converter in html, css, and, java, bootstrap and jquery
Favicon
$( "#x" ).prop( "checked", false ) is not working
Favicon
5 Top Libraries Each Frontend Developer Must Know
Favicon
Ditch the jQuery Crutch and Embrace Vanilla JS πŸ‘¨πŸ½β€πŸ’»
Favicon
Level Up Your Web Development: Why You Should Ditch jQuery for Vanilla JavaScript
Favicon
Why Does jQuery or a DOM Method Like `getElementById` Fail to Find an Element?
Favicon
SimpleTimepickerRB: A Lightweight and Customizable Timepicker Plugin for jQuery
Favicon
A Comprehensive Guide with XHR, Fetch API, Axios and jQuery AJAX
Favicon
Unleash Your Web Development Skills with the 'Quick Start with jQuery' Course
Favicon
Building a One-Page CRUD Application with Laravel and jQuery
Favicon
Here's How I implement cursor-based pagination in jQuery Datatable.
Favicon
Using jQuery
Favicon
HeatColor UDF (based on jQuery library)
Favicon
Jquery select2 css height problem fixed
Favicon
Why jQuery Exists
Favicon
Scientific problems are not real problems for programmers
Favicon
Master jQuery.each() with These 5 Essential Examples
Favicon
Master jQuery.each() with These 5 Essential Examples
Favicon
Total.js UI :Two Beginner Projects to understand Paths and Data Binding
Favicon
Implementing Infinite Scroll with Laravel and jQuery
Favicon
jQuery vs React – Which One Is Better For Your Project?
Favicon
Tutorial Menggunakan jQuery pada WordPress dengan benar
Favicon
Uncovering the Magic: 10 Fascinating Facts About jQuery
Favicon
Asynchronous Requests in JavaScript: AJAX, JQUERY, FETCH and AXIOS.
Favicon
jQuery's Role in Modern Web Development: Beginnings, 2024, and Beyond
Favicon
Vanilla JS Effect Methods
Favicon
Common JavaScript "event Handler" Mistake
Favicon
Recommended Project: Implement User Login Function

Featured ones: