I want that diffrent events to call the same function. I read other post, from here, but I want use this function just in one page.
The situation is something like this:
I have approx. 80 listbox(select) in a form, and I feed them throught a plugin, jCombo (jQuery&jSON&php). First I relate each select listbox (.change()) to a hidden field, because I want to store index on table (update method is made with php). The problem is when I reopen same record for modify(same php file), jCombo change all <select lists> and reinitialize to 0. So my my event $("select#listbox").change({}); change, obviously, my hidden field value to 0.
Having this situation I find a simple solution (I start use jQuery&Ajax three days ago) to unrelate function from select lists and relete to $(“#AddButton”).onmouseup({…});.
So, I write for 80 hidden field:
if ($("select#listbox_n_").val() != 0) {
$("#hidden_n_").val($("select#listbox_n_").attr('value'));
}
,where n=1…80.
But now I must repeat this for Update button so:
- there is other method to address a function by name?
- what is the best place (eg.procedure or something) to “store” function, because I want to execute them, only if it’s call.
I’m guessing what you’re trying to do is call one function from multiple events? If so, all you need to do is write your function and reference it in your events. And as for placing the function, just place it outside of your document.ready (
$(function() {...});).Just to illustrate, here’s something I happened to be writing when I read this question. This checks the length of a textarea field, and returns it. This is just plain and simple HTML/jQuery.
HTML:
jQuery:
As you can see, the function
checkLength()is called twice, on both the keyup and keydown events from the textarea. Note that, like I said above, they are placed OUTSIDE of the document.ready function. It then returns a string that contains how many characters have been added, compared to the maximum amount. Last, but not least, note that the function is called WITHOUT (). This means that it is just a reference to the function, and it will only be called when that specific event is fired.I hope this helps!