views:

458

answers:

2

Hi Folks,

$(document).ready(function(){
    var _new_li     = $('<li/>',     {
      'id':     'p',
      'text':   'CLICKME',
      click:    function(){
          alert('fired');
      },              
      data:     {
          'somedata':  'somedata',
      }            
    });

_new_li.appendTo($("#example"));
});

I receive an "Uncaught TypeError: Cannot read property 'click' of undefined", when I try to click the element which I created like so. But, if you switch click: and data: it works.

$(document).ready(function(){
    var _new_li     = $('<li/>',     {
      'id':     'p',
      'text':   'CLICKME',
      data:     {
          'somedata':  'somedata',
      },
      click:    function(){
          alert('fired');
      }                      
    });

_new_li.appendTo($("#example"));
});

any explanation for that behavior?

Kind Regards

--Andy

PS:

I posted a similar behavior earlier in the jQuery Core Development forum, Mr. Swedberg answered there:

I'm pretty sure this is happening because you're setting data with an object, which >(until 1.4.2) would overwrite the event object. Not sure which version of jQuery you're >using in your project, but it looked like the jsbin example was using 1.4. Try upgrading >to 1.4.2 and see if that helps.

But it seems like the problem still exists in 1.4.2

+4  A: 

Post rewrited.

You shouldn't put that comma after last (and only) element in data.

After trying some stuff I got to this:

$(document).ready(function(){
    var fun=function(){
          alert('fired');
      };
    var parms={
      'id':     'p',
      'text':   'CLICKME',     
      'click':fun,         
      'data':     {
          'somedata':  'somedata'
      }
      };
      console.log(parms);
    var _new_li = $('<li/>',parms);

_new_li.appendTo($("#example"));
});

Everything works fine until I click on the li element. Then I get e is undefined (jquery line 55). Works well when click and data are swapped.

Still investigating

AND FOUND IT

jquery development version, line 1919

var events = jQuery.data(this, "events"), handlers = events[ event.type ];

events is undefined.

jquery overwrites events stored in data.

so this IS a bug. It should just extend.

I've submited a bug report.

naugtur
"Try 'click' instead of click as it might be coliding with a variable." -- What? It's to the left of the colon and is therefore resolved as an identifier -- having a variable with the same name in the same scope makes no difference.
J-P
And also, jQuery does not change ANY native/host objects.
J-P
-1 1) there is no trailing comma, 2) 'click' should make absolutly no difference. see http://jsbin.com/akeyo/3/edit
jAndy
@J-P Yes, but He might have used something ELSEAs for the click - it was when I couldn't reproduce
naugtur
@jAndy there IS a comma here: `'somedata',`. I'm trying to help. Is that wrong?
naugtur
removed the -1 after you edited your post. Well, I thought they fixed the 'overwrite' in 1.4.0.
jAndy
where did you submit the bug report?
jAndy
"jquery overwrites events stored in data." -- I'm having trouble finding where it does this. If this were the case then presumably `.click(fn).data({})` would also fail, but it doesn't...
J-P
I posted a similar behavior earlier in the jQuery Core Development forum, Mr. Swedberg answered there:'m pretty sure this is happening because you're setting data with an object, which (until 1.4.2) would overwrite the event object. Not sure which version of jQuery you're using in your project, but it looked like the jsbin example was using 1.4. Try upgrading to 1.4.2 and see if that helps. But it seems like the problem still exists in 1.4.2
jAndy
@jAndy posted http://dev.jquery.com/ticket/6484, my tests were on latest jquery@J-P Line I copied does jQuery.data(this, "events") and gets nothing. If putting data after event causes this to happen then it's rather obvious. (and it turns out to be a known problem)
naugtur
@J-P And to be clear. `.click(fn).data({})` does not fail becouse it's after element is created and this little chain of Yours does different things than the example posted in the question.
naugtur
A: 

I ended up modifying the source code slightly, so it would not randomly blow up here.

handle: function( event ) {
...

// tbn modified source
try {
   var events = jQuery.data(this, "events"), handlers = events[ event.type ];
} catch(e) {}
// tbn modified source

..then re-minified the source

I have not had any problems with this since (yet).

tim