0

Let's say I have the following div.

<div id="123" class="abc">Foobar</div>

I know I can do this to trigger a function when this div is moused-over:

$(".abc").bind({
    mouseenter : SomeFunction(id)
});

With this, before SomeFunction runs, I want to be able to extract out the id of this div, which is "123", and pass it though as a parameter to SomeFunction for it to handle. Could I do that?

6 Answers 6

4
$('.abc').bind('mouseenter', function() {
    SomeFunction($(this).attr('id'));
});

Or, if you really want the event map syntax:

$('.abc').bind({
    mouseenter: function() {
        SomeFunction($(this).attr('id'));
    }
});
1
  • Thanks! Worked like a charm!!
    – eastboundr
    May 14, 2012 at 18:40
2
$(".abc").bind('mouseenter', function() {
  var id = this.id;
})

according to you question

$(".abc").bind({

 mouseenter : function() {
                SomeFunction(this.id)
              }

});
2
$(".abc").on({
   mouseenter : function() {
      var id = this.id;
       //or just
      SomeFunction(this.id);
   }
});

As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document. For earlier versions, the .bind() method is used for attaching an event handler directly to elements.

1
$(".abc").bind('mouseenter', function() {
    var id = $(this).attr('id');
});
1
$('.abc').bind('mouseenter', function() {
    SomeFunction(this.id);
});
1

You don't need to pass it to the function. There is a simple way.

$(".abc").bind({
    mouseenter : SomeFunction /* This might be a new way for you. 
                                 But its very useful sometimes */
});

function SomeFunction()
{
    var id = $(this).attr("id"); /* You can access $(this) in this function. 
                                    Here the $(this) will refer to the bound 
                                    element (i.e. of class ".abc") 
                                 */
}

Simple!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.