TransWikia.com

How to change the order of jquery events?

Stack Overflow Asked by Turqay Umudzade on October 2, 2020

I have 2 events here one is click and one is focus on the input, I need the click to run before focus so I added a timeout to it but I feel like this isn’t an elegant solution.

  $(document).click(function(e) {
        var target = e.target;
        var lastElement = '#' + lastList;
        console.log('here first');
        if (!$(target).is(lastElement) && !$(target).parent().parent().is(lastElement) && !$(target).parent().parent().parent(lastElement).is(lastElement)) {
            $('.add-card .fa-plus').show();
            $(lastElement).children().children().children('textarea').addClass('pl-8 bg-gray-200');
            $(lastElement).children().children().children('textarea').removeClass('h-20 px-4 w-60 resize bg-white');
            $('.js-add-card-button').addClass('hidden')
        }
    });
    
    $(document).on('focus', '.js-create-card', function(e) {
        setTimeout(() => {
            console.log('here');
            $(this).siblings('.add-card .fa-plus').hide();
            $(this).removeClass('pl-8 bg-gray-200');
            $(this).addClass('h-20 px-4 w-60 resize bg-white');
            $(this).siblings('.js-add-card-button').removeClass('hidden');
            lastList = $(this).parent().parent().parent().attr('id');
        }, 100);
    });

So I have a few questions about the solution I used:

  1. Is there a async-await solution to this problem?
  2. The 100ms delay works for me but will it work on all PCs? Can a slower PC ruin this structure?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP