Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar does not trigger jQuery events #17515

Closed
bembelimen opened this issue Aug 12, 2017 · 17 comments
Closed

Calendar does not trigger jQuery events #17515

bembelimen opened this issue Aug 12, 2017 · 17 comments

Comments

@bembelimen
Copy link
Contributor

bembelimen commented Aug 12, 2017

Steps to reproduce the issue

  1. Open an article
  2. Apply an onChange jQuery event to the publish down field
    You could use the console and insert:
    jQuery('#jform_publish_down').on('change', function() { alert('test'); });
  3. Change the date of the publish down event

Expected result

The event will be triggered (=> alert test)

Actual result

No trigger

Additional comments

I'm not sure, why there is a vanilla JS calendar without any jQuery hooks in a system which relays on jQuery...
To check the event use:
jQuery('#jform_publish_down').trigger('change');

Delegation should probably work, too

@dgrammatiko
Copy link
Contributor

dgrammatiko commented Aug 12, 2017

@bembelimen why not:

document.getElementById('jform_publish_down').addEventListener('change', function() { alert('test'); });

??

This works in IE8 and all other browsers, so honestly I don't see a reason to mix jQuery when native javascript fulfils all our needs.

Any particular reason why we need to care for jQuery's own events?

@bembelimen
Copy link
Contributor Author

Hi @dgt41 ,
Thank you for your answer!

Could you also say something about event delegation?

(The reason for jQuery events is, that with vanilla JS, I have to implement a mapping for the events...)

@dgrammatiko
Copy link
Contributor

@bembelimen actually looking at the code I didn't see any code for triggering the change event, or I was too hurry. I guess if the event is triggered (vanilla or jQuery) will eventually bubble up to jQuery. Give me few days and I will come back with some more solid answers here

@bembelimen
Copy link
Contributor Author

@bembelimen
Copy link
Contributor Author

@dgt41 any updates here?

@C-Lodder
Copy link
Member

C-Lodder commented Sep 8, 2017

@bembelimen

You can add the onchange event listener to the calendar form field, like so:

<field
    type="calendar"
    onchange="Joomla.inputHasChanged();"
/>

Then in your JS:

var Joomla = Joomla || {};
   
(function(Joomla) {
    Joomla.inputHasChanged = function() {
        alert('changed!');
        // You can do some more jazzy stuff here.
    }

    document.addEventListener('DOMContentLoaded', function() {
        Joomla.inputHasChanged();
    });
})(Joomla);

@bembelimen
Copy link
Contributor Author

Yeah, but I don't understand why there is no default implementation....

@C-Lodder
Copy link
Member

C-Lodder commented Sep 8, 2017

@bembelimen The calendar was a huge change, must have been missed out.

@dgrammatiko
Copy link
Contributor

@bembelimen @C-Lodder well it didn't exist in the old calendar and nobody ask for it during the testing period, but it's fairly simple to add a listener for an event, or dispatch an Event from the calendar.

@bembelimen
Copy link
Contributor Author

@dgt41 do you have any plans to implement it? That would be great!

@coolcat-creations
Copy link
Contributor

Is still still open? Any hint how to fix it?

@dgrammatiko
Copy link
Contributor

@coolcat-creations supporting jQuery events in 2020 is not very bright idea. It wasn't back in 2017...

@C-Lodder
Copy link
Member

Events are events. Doesn't matter if they're called in jQuery or vanilla JS.
I'll start looking into this now.

@C-Lodder
Copy link
Member

C-Lodder commented May 13, 2020

The problem being is that the value is being set via Javascript, so the normal event listener don't apply. You can add a custom event:

https://github.com/joomla/joomla-cms/blob/4.0-dev/build/media_source/system/js/fields/calendar.es5.js#L236

Add the following to Joomla's core calendar:

const event = new Event('change');
this.inputField.dispatchEvent(event);

Then in your own code:

element.addEventListener('change', () => {
    console.log('has changed')
}, false)

jQuery('#element').on('change', () => {
    console.log('Look Mum....I can use jQuery too!')
})

@ReLater
Copy link
Contributor

ReLater commented Jul 22, 2020

Comment #17515 (comment)
by @dunkmark
is SPAM. Please delete.

@Fedik
Copy link
Member

Fedik commented Mar 20, 2021

please test #32756

@alikon
Copy link
Contributor

alikon commented Mar 20, 2021

closing as we have a pr #32756

@alikon alikon closed this as completed Mar 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants