Git Product home page Git Product logo

Comments (6)

RongBranovate avatar RongBranovate commented on July 17, 2024

I also tried to use an event handler as so:

function foo(){
  var buttons = $(document.body).find('.elem1 , elem2');

  var myEventHandler = function(){
    //some code...
    buttons.off('click', myEventHandler);
  }

  buttons.on('click', myEventHandler);
}

but with no avail, the result was the same as i got with the one() function where only the pressed button got the event removed from it

from jqlite.

jgermade avatar jgermade commented on July 17, 2024

Hi @RongBranovate, this is a hard question.

It was hard because handler is embeded into a function autoDetach(handler) wrapper. So the handler is not the function that is attached.

Finally I've created a onceListeners collector and is passing tests for release: v0.2.12

please tell me if is working for you. And thanks, as always!!

from jqlite.

RongBranovate avatar RongBranovate commented on July 17, 2024

Hi @jgermade, this is still an issue.

I am using the first method i describered and from what i'm seeing, in jqlite.js lines 1242-1245:

  var index = onceListeners.indexOf(listenerData);
          if( index >= 0 ) {
            onceListeners.splice(index, 1);
          }

If i have 2 elements with that event the onceListeners.splice only removes one of them(the one that got pressed), the other one is still assigned and will fire when pressed.

from jqlite.

jgermade avatar jgermade commented on July 17, 2024

Hi @RongBranovate , I think this is the expected behavior (like jquery).

Maybe this snippet can help you:

demo in plunker

<div>
      <button class="once">Button 1</button>
    </div>
    <div>
      <button class="once">Button 2</button>
    </div>
    <div>
      <button class="once">Button 3</button>
    </div>
    <div>
      <button class="once">Button 4</button>
    </div>

    <br/>

    <div>
      <button class="reset">Reset</button>
    </div>
$(function () {

  var jButtons = $('button.once');

  function clickOnce (e) {
    alert('clicked ' + e.target.textContent);
    jButtons.off('click', clickOnce);
  }

  jButtons.one('click', clickOnce);

  $('button.reset').click(function () {
    jButtons.off('click', clickOnce);
    jButtons.one('click', clickOnce);
  });

});

from jqlite.

RongBranovate avatar RongBranovate commented on July 17, 2024

Hi @jgermade

YES! This worked! thanks alot!

from jqlite.

jgermade avatar jgermade commented on July 17, 2024

thanks you @RongBranovate !!

from jqlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.