views:

178

answers:

3

Hiya

Not really much of a JS man - so can anyone explain why this isn't working? I want to call a predefined function in one script from my simple AJAX script - I just want it to call the qtip functionality upon the contents of the div brought into the page via AJAX.

Thanks, H

SCript 1 - AJAX loader

       if(myHttpRequest.readyState==4)
            data.innerHTML = myHttpRequest.responseText;
    qtip_me('.div_to_act_on');
  }

Script 2 - main JQuery script

$(document).ready(function() {

    function qtip_me(a) {
     $(a).each(function() {
        $(this).qtip({ 
         content: {'showme'},
       position: {corner: {tooltip: 'bottomLeft', target: 'topRight'}},
       style: { 
          width: 300,
          padding: 5,
          background: '#A2D959',
          color: 'black',
          textAlign: 'center',
          border: {
          width: 7,
          radius: 5,
          color: '#A2D959'
          },
          tip: 'bottomLeft',
          name: 'dark' // Inherit the rest of the attributes from the preset dark style
       }   
      }); 
     });
    };
});
+1  A: 

You do not need to nest your qtip_me() function inside of the $(document).ready block. It is a generic function call, not one that is reliant on the document being fully loaded. This is because it is assumed your previous AJAX request had occurred after the document was loaded (could be a false assumption).

cballou
+2  A: 

It might be that the function defined like that is not in scope. Try defining the function like so:

qtip_me = function(a) {}

This way it will be in scope.

Or as the other answers say, don't define the function inside the document ready.

tharkun
@tharkun: "jQuery scope" was friggin annoying. There is no such thing. Also, your syntax was wrong. But essentially your *idea* was correct, so I removed my dv.
Crescent Fresh
@tharkun: there you go again with the "jQuery scope" crap. -1
Crescent Fresh
why so aggressive @cf? your language?
tharkun
this is a totally valid answers which provides a valuable addition to the other answers, it doesn't diserve downvoting.
tharkun
@tharkun: you are right, it doesn't deserve a downvote now after the edits, so I've rescinded my -1. But prior to the edits, it did. It flat out didn't work. And therein lies the value of a downvote on SO: it makes you look at your answer again, and scrutinize it so others don't do it for you.
Crescent Fresh
@CreFre: agreed!
tharkun
+2  A: 

It is declaring the qtip_me function in lexical scope, the function cannot be accessed from outside it's enclosing function.

Jacob Relkin