Jump to content
joymis

jquery .trigger() add extra parameters to the event type

Recommended Posts

Hello,

I have this code

<button class="btn btn-info" onclick="BatchAsign()">Batch</button>

<button class="asign_btn" onclick="Asign({a:'1', b:'2'}, this)">Asign1</button>
<button class="asign_btn" onclick="Asign({a:'2', b:'6'}, this)">Asign2</button>
<button class="asign_btn" onclick="Asign({a:'5', b:'4'}, this)">Asign3</button>

<script>
function BatchAsign(){
	$.each( $('.asign_btn'), function( key, val ) {
		$(this).trigger( "click", ['all'] )
	});
}
  
function Asign(arr, dom, x)
{
  if(x == 'all'){
    // I want do something
  }
}
</script>

When I click BatchAsign button, I want pass a extra parameters to Asign.

I used console.log(x), it's display 'undefined'.

How can I get string 'all' in the function Asign?

thanks.

Edited by joymis

Share this post


Link to post
Share on other sites

The jQuery documentation for trigger seems to imply that you need to use the on method or one of its shortcuts to assign the event handler.  That might mean it's not going to work correctly by using event attributes.

Share this post


Link to post
Share on other sites

Use a custom data- attribute on 'asign_btn' class to add to and store the value 'all' when batch button clicked, then use dom parameter to retrieve that value from that specific data attribute.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×