Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply multiple events to the same function

I'm not the best at this jquery stuff. But I'm trying to seperate the action from the function so I can apply multiple events that cause the same function. Unfortunately this isn't working. Anyone know why?

Updated Function, but still errors

$(document).ready(function() {

var $info_items = jQuery('.checkbox.has_info, .has_info');

$info_items.click(function(event) {
$(this).show_text(event);
});


// I suspect it has something to do with this initalizer of the function here 

jQuery.fn.show_text = function(event){
  var $info_item = jQuery(this);
  $info_items.filter(function(index){
    return $(".hidden_text").css("display","block");
    }).not($info_item).parent().next().next().hide("slow");
  $info_item.parent().next().next().show("fast");
});

});
like image 943
Trip Avatar asked Feb 13 '26 12:02

Trip


2 Answers

What is e, the event? You need to name the event argument to the click() function to use it. Also, to invoke show_text such that it has a this, you need to invoke it on an element:

$info_items.click(function (event) {
  // 'this' is the element in $info_items which was clicked

  // invoke show_text on the element in question
  $(this).show_text(event);
});

You also have an extra ) on your final }); line.

like image 171
meagar Avatar answered Feb 16 '26 04:02

meagar


You can use jQuery bind to attach several events to a single function.

$('#whatever').bind('mouseover focus click', function() {
   your_custom_function();
});
like image 34
Frankie Avatar answered Feb 16 '26 04:02

Frankie