0

According to the jquery manual:

.triggerHandler( eventType [, extraParameters ] )

where extraParameters is an array

If I have passed only one parameters ok but if I try to pass multiples parameters not work. Only first parameter of array is available

	$('#test')
	
	.off('test_event')
	
	.on('test_event',function(event,parameters){
			console.log(parameters); // return 1 not array
		})
		
	.click(function(){
		$(this).triggerHandler('test_event',[1,2,3,4]);
	})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id = "test">
	test
</button>

2 Answers 2

2

The number of arguments in your click handler needs to match what you're passing it. When a normal click event fires, all the arguments will be undefined, but when you trigger it and pass the array, the values are separated into individual parameters.

$('#test')
  .off('test_event')
  .on('test_event', function(event, a, b, c, d) {
    console.log(a, b, c, d);
  })
  .click(function() {
    $(this).triggerHandler('test_event', [1, 2, 3, 4]);
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="test">
	test
</button>

Sign up to request clarification or add additional context in comments.

1 Comment

I have accepted @Obsidians answer because He has been first () ( 30 seconds before )
1

Elements passed in an array in a click handler are separated out into independent parameters. As such, they need to be assigned to the function() itself. You have four elements in your array, so you need to pass four variables to the function:

$('#test')
  .off('test_event')
  .on('test_event', function(event, a, b, c, d) {
    console.log(a, b, c, d);
  })
  .click(function() {
    $(this).triggerHandler('test_event', [1, 2, 3, 4]);
  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="test">
	test
</button>

Hope this helps! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.