- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to return variable value from jQuery event function?
You cannot return variable value from jQuery event function. To understand the reason, let us see how data is passed, which was created in the event handler of a button click.
Example
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ var a=document.getElementById('demo'); a.addEventListener('click', function(){ var s='Hello World!'; var event = new CustomEvent('build', { 'detail': s }); this.dispatchEvent(event); }) document.getElementById('parent').addEventListener('build', getData, true); function getData(e){ alert(e.detail); } }); </script> </head> <body> <div id='parent'> <button id='demo'>Click me</button> </div> </body> </html>
- Related Articles
- How to return a value from a JavaScript function?
- How to trigger a hover event from another element using jQuery?
- How to override jQuery event handlers?
- How to return void from Python function?
- How to return table from MySQL function?
- How to get the return value from a function in a class in Python?
- Create global variable in jQuery outside document.ready function?
- How to remove event handlers using jQuery?
- How to suppress jQuery event handling temporarily?
- How to handle jQuery AJAX success event?
- How to call a JavaScript function from an onClick event?
- How to call a JavaScript function from an onsubmit event?
- How to call a JavaScript function from an onmouseover event?
- How to call a JavaScript function from an onmouseout event?
- How to get value from serialized array in jQuery?

Advertisements