tags:

views:

140

answers:

2

Hi,

how is that possible that call to the alert function changes behavior of a js script? Something like that:

function add_token (item) {
    var li_data = $.data(item.get(0), "tokeninput");

works but this:

function add_token (item) {
    alert('a');
    var li_data = $.data(item.get(0), "tokeninput");

doesn't - I get following error in firebug: li_data is undefined jquery.tokeninput.js Line 336 (adding alert call is the only change)

(as you can see I'm playing with Tokenizing Autocomplete jQuery plugin - http://loopj.com/2009/04/25/jquery-plugin-tokenizing-autocomplete-text-entry/ )

Now that's something that bugs me. Can someone explain to me how is that even possible?

UPDATE: I tried using Chrome instead of Firefox and this particular problem doesn't appear. So now what? It should be considered some kind of a bug in FF or is there something else about this that I'm not aware of?

+1  A: 

Pure speculation, but the fact that it's for an autocomplete thing makes me wonder if the fact that alert is stealing the focus is screwing anything up. Is there perhaps an onblur event that is firing at the wrong time?

Sean Nyman
+1  A: 

I agree with Darth - probably a focus related issue.

If you need to see value of your script as it executes, use Firebug instead

function add_token (item) {
    console.log('a');
    var li_data = $.data(item.get(0), "tokeninput");
Peter Bailey
Yes - you are both right. There was blur event fired which set item to null. :)The reason it worked in Chrome was that Chrome executed blur event after li_data was read.Thanks - I'm just starting with js and it would take me looong time to come up with the answer myself.
Olek