I have the following:
$('#EID').change(function () {
$.ajax({
url: "/Administration/stats",
data: { DataSource: $('#DataSource').val(),
EID: $('#EID').val()
},
success: function (data) {
$('#TID').html(data);
}
});
});
This works good but I want to be able to call the function () at other times and not just when EID changes. Can someone show me how I can pull out the function code into a separate block with a name and then call that function name.
Answer: Use the syntax $. fn. myFunction=function(){}
The idiomatic way to use named functions is to do: function doSomething(thing){ return thing + 1 } doSomething(3);
You can directly write your jQuery code within a JavaScript function. See below code. I have placed a button on my page which makes a call to a Javascript function "Test()".
In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements: 1. $( "div.
function doSomething() {
$.ajax(...);
}
$('#EID').change(doSomething);
Note that you must not add ()
after the function name since you want to pass the function, not its return value.
In case you wanted to pass some parameter to the function, you'd do it like this:
function doSomething(someParam) {
$.ajax(...);
}
$('#EID').change(function() {
doSomething(whateverSomeParamShouldBe);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With