UPDATE: As of jQuery 1.7+, the event object (e
in the code below) object will contain a property named e.isTrigger
which is true
if the event was triggered and undefined if not triggered; this is undocumented so check this (demo) before using it. If using an older version of jQuery, use the code below.
It might just be easier to pass a flag to your function. Here is a demo:
HTML
<button>Trigger a click below</button>
<div id="test">Click Me</div>
Script
$('#test').click(function(e, triggered){
var msg = (triggered) ? ', triggered' : '';
console.log('clicked' + msg);
});
$('button').click(function(){
// pass a true flag to the click function to indicate
// it's been triggered
$('#test').trigger('click', true);
});
Update: data-attributes can contain valid JSON which is automatically converted into an object (demo):
<div class="test" data-args='{ "quantity": 1, "type": "pizza", "extra1": "peperoni", "extra2": "cheese", "extra3": "cheesy bread" }'>Click Me</div>
Note that the data-arg
uses a single quote to contain the JSON, but the JSON inside MUST use double quotes around each key and value (unless it's numeric).
Then use the data method to extract the information:
var args = $(this).data('args');
Alternatively, you can make a separate data-attribute for each argument (demo):
<div class="test" data-quantity="1" data-type="pizza" data-extra1="peperoni" data-extra2="cheese" data-extra3="cheesy bread">Click Me</div>
Then use gather all of the data from the element as follows:
var args = $(this).data();