Overload a JS plugin function with a function from inside - javascript

I want to call a function which is in my plugin.
I overloaded a function but I want to call another function from it.
$("mySelector").Myplugin({
My_function : function (){
*do some stuf*
function_from_the_plugin();
}
});
An error appears that says "Unknown function". I think it do not search the function in the plugin but outside.
Do you know how I can do?
I already try with "this", a variable corresponding to my plugin object.
If you have an idea please share it with me.
Thank you for reading this post.

Add this.function_from_the_plugin(); in code where Myplugin() is initialized.

Related

call jquery document.ready function inside another jquery or javascript function

I am facing one problem
I am trying to call document.ready() function inside another JavaScript function but when I am calling it, it says "function is not defined
Here is my code :
this is first function that is document.ready() function
$(document).ready(function ExpandMessageBox() {// this is my document.ready function. I have given the name of it as "ExpandMessageBox"}
this is another regular JavaScript function in which I am trying to call above function
function ShowMessageBox() {
ExpandMessageBox();}
But this is giving me an error as :
"Uncaught ReferenceError: ExpandMessageBox is not defined"
Where I am doing wrong...
Please Help
Tons of thanks in advance
You need to define ExpandMessageBox function in the common scope. Something like this:
function ExpandMessageBox() {
//your staff
}
function ShowMessageBox() {
ExpandMessageBox();
}
$(document).ready(ExpandMessageBox);

$.getJSON callback does work

I am experimenting with jQuery and I try to use the $.getJSON function
I red that "A reference to a function declaration could equally be provided as the callback". So instead of an anonymous function, I use a reference to a function declaration like this
$('#letter-b').click(function(e) {
e.preventDefault();
$.getJSON('b.js', outside(data));
});
function outside (data){alert(data);}
I get no alert. Instead I get Uncaught ReferenceError: data is not defined
What am I missing? Is it my syntax?
Thanks in advance
You must give the reference to your function. Like this :
$('#letter-b').click(function(e) {
e.preventDefault();
$.getJSON('b.js', outside);
});
If you write outside(data), you just execute your function.

Trouble with calling a function

I am trying to call a function inside of a button click event.
$('#btn_save').button().click(function(){
executeStatement();
});
$.fn.executeStatement = function(){
alert("Here!!!");
$.ajax({
url: 'api.php',
data: sqlStatement,
dataType: 'json',
success: function(data)
{
alert("Success!!!");
}
});
}
The problem is that it never gets called because it is undefined according to the debugger: Uncaught ReferenceError: executeStatement is not defined
Can someone give me a hint what is wrong?
Thanks in advance
Unless you are trying to make a jQuery plugin (which I don't think you are?), change your function declaration to:
function executeStatement(){
//code here
}
Just to elaborate on the previous answer, $.fn is kind of like jquery's namespace. Anything that starts with $. is part of jquery. You can actually add functionality to jquery by adding functions to $.fn, but it's probably not what you want.
I found this really confusing at first too, so I thought I'd try to clarity.
Curt's answer is completely correct. But I thought I would add to it.
function executeStatement(){}
this is called a function declaration and will work in this case.
var executeStatement = function(){};
this is called a function expression and will also work in this scenario.

Passing argument to function. What's wrong?

In this jsFiddle
am I trying to pass an argument to a function, but it doesn't receive the argument or it isn't executed.
Details
JQuery
$(document).ready(function() {
function addRemove(u) {
alert(u);
}
});
Any ideas what's wrong and how to fix it?
Your function only exists within the scope of the ready event handler, you need to move function addRemove outside of the ready function.
http://jsfiddle.net/EcCTx/2/
Your code was wrapped in an onload event by jsfiddle (drop-down menu on the left). So if you add a function it won't be global, but your onclick event calls a global function by the name addRemove.
You need to define your function outside of the $(document).ready().
I haven't tested it, but my guess is this: things inside of a function can't be accessed from outside of a function. For example,
$(document).ready(function() {
function addRemove(u) {
alert(u);
}
});
console.log(addRemove); // reference error or something similar
You should define addRemove function outside of $(document).ready.
the addRemove function must be outside of $(document).ready(function(){...});
In case Davin doesn't come back, here's the answer: jsFiddle defaults to wrapping your JS in the 'onLoad' method - and we can't allow that.
http://jsfiddle.net/nqbWe/
You had no defined function called addRemove in the Fiddle!
I've added this, and removed the inline javascript calls.
See this for better way of doing it:
http://jsfiddle.net/EcCTx/6/
There is nothing specifically calling that function. In the document ready part you have the function set up, but the anchor will not call that function by itself. In this instance it will only be called when someone clicks on that link.
You could give the link a class and data attribute and use those with jQuery to have something happen on page load.

IE6 textBox.focus(); causing "Unexpected call to method or property access"

The issue is now resovled :) Thanks for everyone's help and attention!
I'm getting the JS error "Unexpected call to method or property access" in IE6 intermittently on the line "oAutoCompleteTextBox.focus();". Hopefully, someone has seen this issue before and can provide some insight on how to solve it. Below is the context of the usage.
$(document).ready(function () {
...
oAutoCompleteTextBox = GetElement('<%=this.AutoCompleteTextBox.ClientID%>');
...
SetupDefaultValues();
}
function SetupDefaultValues() {
...
if(canFocus(oAutoCompleteTextBox)) {
oAutoCompleteTextBox.focus();
}
}
My 1st post on stackoverflow - YAY!
OK, so the issue was that the jQuery $(document).ready() event isn't fired on updatepanel async postbacks. The solution is to refactor the function definition inside the ready() into an explicit function definition (i.e. function pageReady(){...}) and add the new pageReady() eventhandler to ASP.net Sys.WebForms.PageRequestManager endRequest event which is only fired on async postbacks.
So the code now looks like this:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(pageReady);
$(document).ready(pageReady);
function pageReady() {
...
oAutoCompleteTextBox = GetElement('<%=this.AutoCompleteTextBox.ClientID%>');
...
SetupDefaultValues();
}
function SetupDefaultValues() {
...
if(canFocus(oAutoCompleteTextBox)) {
oAutoCompleteTextBox.focus();
}
}
Thanks for everyone's help and attention - took a while to figure out, I'm just glad it's resolved :)
Is oAutoCompleteTextBox declared globally? You're setting it in the document.ready function but trying to use it in another function.
are you sure its a textbox? what does "canFocus" function do? alert on that line, oAutoCompleteTextBox.tagName, then if it is "INPUT" alert .type, if it is "text" then you have problem :) knowing IE6, it might be a timing problem nothing but, if you call setupdefaultvalues in a settimeout of 10 seconds, i MIGHT work

Categories