javascript - e.preventDefault(); undefined is not a function - Stack Overflow

I'm getting the following error that is saying my e.preventDefault(); ---> "e." undef

I'm getting the following error that is saying my e.preventDefault(); ---> "e." undefined is not a function when clicking <button class='url_qry_add' onclick='url_qry_add(this);'>. The function itself is defined before the end of my </body> and I have only invoked jQuery once.

The function structure is as follows:

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 afterwards were not being picked up.

So I've been trying to figure out how to go about it and decided I should try converting the problem function to a named "invokable" function and putting the call in manually with the onclick='..' into the buttons that exist before and after dynamic creation.

Like I say, the error must be in the way I've created the function or the way I'm calling it. The error can't be to do with the order of files and I have not accidentally nested the function within another function or a document.ready.

What am I doing wrong?

I'm getting the following error that is saying my e.preventDefault(); ---> "e." undefined is not a function when clicking <button class='url_qry_add' onclick='url_qry_add(this);'>. The function itself is defined before the end of my </body> and I have only invoked jQuery once.

The function structure is as follows:

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 afterwards were not being picked up.

So I've been trying to figure out how to go about it and decided I should try converting the problem function to a named "invokable" function and putting the call in manually with the onclick='..' into the buttons that exist before and after dynamic creation.

Like I say, the error must be in the way I've created the function or the way I'm calling it. The error can't be to do with the order of files and I have not accidentally nested the function within another function or a document.ready.

What am I doing wrong?

Share Improve this question edited Nov 7, 2014 at 8:14 Jonathan asked Nov 7, 2014 at 8:03 JonathanJonathan 11.5k9 gold badges69 silver badges83 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6
<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 clarify how it works "internally", when we add attributes to function url_qry_add "inside" 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 variable "event" (event object, where we have method preventDefault and so on), and "this" (current element). I hope that this explanation will help you understand where we get variable "event".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744291944a4567074.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信