user2320239
user2320239

Reputation: 1038

How to get event string?

Hi I'm writing some Javascript that triggers a function when a div is clicked and then prints the name of the event that triggers it so I have:

$('#areaOne').on('show.bs.collapse', onAreaClick);

function onAreaClick(){
    console.log(this.id + ' ' + event.trigger)
}

However when I run this I get 'areaOne undefined' when I'd like 'areaOne show.bs.collapse'. Any help would be greatly appreciated.

Upvotes: 1

Views: 155

Answers (3)

adeneo
adeneo

Reputation: 318342

You have to first reference the event argument, and then you probably wanted the event type

$('#areaOne').on('show.bs.collapse', onAreaClick);

function onAreaClick(evt){
    console.log(this.id + ' ' + evt.namespace + '-' + evt.type)
}

FIDDLE

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74410

The handleObj event property will expose all you need BUT you have to pass event as handler parameter because, e.g, FF doesn't use global event model:

$('#areaOne').on('show.bs.collapse', onAreaClick);

function onAreaClick(e){
    console.log(this.id + ' ' + e.handleObj.type + '.' + e.handleObj.namespace)
}

Upvotes: 1

Bilbo Baggins
Bilbo Baggins

Reputation: 1199

Try this:

$('#areaOne').on('show.bs.collapse', onAreaClick);

function onAreaClick(event) {
    console.log(this.id + ' ' + event.trigger)
}

edit: adeneo's answer is correct.

Upvotes: 0

Related Questions