Scarface
Scarface

Reputation: 3923

live function undefined if I call it

I have a live function which I also want to be able to call. I realize I cannot call it but I am not sure how to approach this. When I try to call the live function it comes up as undefined. Anyone have any suggestions on how to handle this? I want to be able to call the function without clicking the #open-window link and pass it different information to open a window when different circumstances occur. Thanks in advance for any assistance.

$('#open-window').live('click', function openwindow() {
var user=$(this).attr('data-name');
//...and then append some information
});

openwindow(somevariable);

Upvotes: 1

Views: 1122

Answers (3)

Jordan Running
Jordan Running

Reputation: 106147

function openwindow() {
  var user=$(this).attr('data-name');
  //...and then append some information
}

$('#open-window').live('click', openwindow);

openwindow(somevariable);

Upvotes: 1

JohnP
JohnP

Reputation: 50029

You're defining your function in a lesser scope than you need it. Reverse it.

function openwindow(somevariable) {
   var user=$(this).attr('data-name');
   //...and then append some information
}

openwindow(somevariable);
$('#open-window').live('click', openwindow);

Upvotes: 2

alex
alex

Reputation: 490657

A named function expression is not meant to leak that name to the outside scope; it should be used only to make the function recursive.

Note that IE erroneously does leak the name.

Upvotes: 2

Related Questions