How do I call a custom defined function from within a jQuery click event?
I define a clearOrder function that executes some actions.
Then, when I click on a DOM element, I want to call that function, and then do some more actions.
The actions are simply applying and removing classes. As you can see I want to clear the specified existing classes from elements as a sort of reset before adding specific classes to the DOM elements.
html
<div id="neti" class="tile"> contents </div>
<div id="sife" class="tile"> contents </div>
javascript
(function() {
clearOrder(function() {
return $('.tile').removeClass("order-1 order-2 order-3 order-4 order-5 order-6 order-7 order-8 order-9 order-10 order-11 order-12 order-13 order-14 order-15 order-16");
});
jQuery(function() {
$('#neti').click(function() {
clearOrder.call;
$('this').addClass("active");
$('#neti').addClass("order-1");
$('#sife').addClass("order-2");
});
});
}).call(this);
The script behavior should be that when I click on an element, it should remove all of the specified classes, then apply certain classes.
edit: changed to the following, but still not functioning.
(function() {
clearOrder(function() {
return $('.tile').removeClass("order-1 order-2 order-3 order-4 order-5 order-6 order-7 order-8 order-9 order-10 order-11 order-12 order-13 order-14 order-15 order-16");
});
jQuery(function() {
$('#neti').click(function() {
clearOrder();
$('this').addClass("active");
$('#neti').addClass("order-1");
$('#sife').addClass("order-2");
()at the end of the function name to invoke it.. likeclearOrder();... there is no need to use.callclearOrder. Can you display where it's defined? (I'm thinking maybe it's scoped so your anonymous function can't get to it, which would cause it to error out).