views:

60

answers:

3

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");
});

});
+7  A: 

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.

meagar
I thought maybe I had to pass an event to it for the 'this' to work. But I've taken it out, and it still errors out. I understand your example, but I'm trying not to do that so that I can have multiple events fire the same function.
Trip
Thanks for updating. I tried this, and it still bombs. If I comment out the entire function, it's good. ;) . So maybe that initializing syntax is wrong on my end?
Trip
Are you executing this inside a `$(function() { })`? What is `$info_items`? Without more info, we cannot help.
meagar
added $info_items above ;) this is also inside a $(document).ready(function() {
Trip
Also, is your browser giving you an error message? If not, run your code through Firefox and install Firebug.
meagar
Running firebug. But it's given me some other random error, and it errors out my entire javascript library instead of telling me where its breaking. :(
Trip
Yup. It was the leftover `)`
Trip
+1  A: 

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

$('#whatever').bind('mouseover focus click', function() {
   your_custom_function();
});
Frankie
+1  A: 

Are you looking for something like this?

var handle = function(event) {
  $(event.currentTarget).show_text(event);
};
$info_items.bind('click blur', handle);
jdc0589
Does the listener have to explicitly come after the function?
Trip
if you define your handler function as a local variable, yes. If you define it as a named function (even if its a local named function), the listener can come first.
jdc0589