PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How attach a click event with chaining method JavaScript

 October 27, 2022     javascript, jquery, jquery-events     No comments   

Issue

How does the click event work in JQuery?

I mean, I learned how to make chaining method but I have no idea of how make the click event like this:

MyLibrary("selector").click(function(){
console.log("hello");
});

I guest that if I learned this, i'll know how does blur, mouseover, etc etc, works as well.

Please I hope you help me learn, I searched for answer but i could not find any or maybe I was asking in the wrong way.

UPDATE

I know how to use jQuery, BUT WHAT I ASKED WAS how can I make it? I mean, I want to create a library and I want to add a click event like jQuery does.

I did chaining method like this:

Find().UserByName("Bob").Write("DivOrInputObject's ID").UserByPass("pass").Write("DivOrInputObject's ID");

But while writing the code, It came to my mind how can I make a click event? In simple words, I want to imitate click() event like jQuery does and add it to my library.


Solution

I am assuming that you are writing your own jQuery. So if your MyLibrary object have selected element under MyLibrary.el then you can have chainable click handler like this:

click: function(handler){
  this.el.addEventListener('click', handler.bind(this.el));
  return this;
},

This will bind this inside the handler to the element itself. Just like jQuery

Here is this code in action: http://jsbin.com/ahinow/1/



Answered By - Mohsen
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing