E.preventDefault (); undefined is not a function
I get the following error: e.preventDefault();
--->
" e.
" undefined is not a function
when clicked <button class='url_qry_add' onclick='url_qry_add(this);'>
. The function itself is defined to the end of mine </body>
, and I only called jQuery once.
The structure of the function looks like this:
var url_qry_add = function ( e ) {
e.preventDefault();
...
};
It used to be:
$( "ul.url_qry" ).on( "click", "li .url_qry_add", function ( e ) {
e.preventDefault();
...
});
But subsequent buttons added dynamically thereafter were not collected.
So, I was trying to figure out how to do this, and decided that I should try to convert the problem function into a named "invokable" function and place the call manually with the onclick='..'
in buttons that exist before and after dynamic creation.
As I said, the error must be in the way I created the function or the way I call it. The error is not related to file order, and I did not accidentally nest the function in another function or document.ready
.
What am I doing wrong?
source to share
<button class='url_qry_add' onclick='url_qry_add(event);'>
var url_qry_add = function (e) {
console.log(typeof e.preventDefault); // function
};
Update:
I'll try to explain how it works "internally", when we add attributes to the url_qry_add function "internally", it looks like this:
document.querySelector('.url_qry_add').addEventListener('click', function (event) {
(function (event) {
url_qry_add(event, this, $(this));
}).call(event.target, event);
});
var url_qry_add = function (event, element, $jElement) {
console.log(event);
console.log(element);
console.log($jElement);
};
Hence, we have a variable "event" (an event object where we have a preventDefault method, etc.) and "this" (the current element). Hope this explanation helps you understand where we get the "event" variable.
source to share