Chapsterj
Chapsterj

Reputation: 6625

Firing native js event from jquery

Is there anyway I can use native javascript to fire an event called onmove which is only for IE. I can get it to work if I do this.

document.getElementById("device-module").fireEvent("onmove");

This issue here is I don't want to have to pass an id into the custom jquery prototype. I already know what the element is I just need to call javascript native fireEvent on that element. Is this possible or do I have to always get the dom element by document.getElementById() before I can use the js native fireEvent method.

  $.fn.custom = function() {  

    return this.each(function() {
          var _this = $(this);
         _this.fireEvent("onmove");

        });

};

Upvotes: 2

Views: 255

Answers (3)

Bergi
Bergi

Reputation: 664620

Why are you creating a jQuery object out of your native element?

      var _this = $(this);

Just use it:

      return this.each(function() {
          this.fireEvent("onmove");
      });

Upvotes: 0

Joseph
Joseph

Reputation: 119847

the value of the this in an each() is the DOM element itself. you can directly access it this way. no need to wrap again in $()

return this.each(function() {
    //the value of "this" in here IS the DOM element
    this.fireEvent('onmove');
});

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

Use this.fireEvent("onmove");

this already points to the DOM element when using the each iterator.

Upvotes: 1

Related Questions