Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send multiple parameters to jQuery click function? [duplicate]

Possible Duplicate:
How to send multiple arguments to jQuery click function?

I want to pass multiple arguments to a Jquery function. Below is the Javascript sample code. I want to convert this javascript function to a jquery function. How can I pass that arguments into a jquery onclick event?

<a onclick="showState('state_name','state_id')">ADD STATE </a>

function showState(state_name,state_id){
openbox_state(state_name,state_id);
}
like image 440
edaklij Avatar asked Feb 20 '26 02:02

edaklij


2 Answers

Perhaps I'd use datasets here:

HTML:

<a data-state_name="state_name" data-state_id="state_id" >ADD STATE</a>

JS:

$(function() {
...
  $('a[data-state_id]').click(function() {
    var $this = $(this);
    showState($this.data('state_name'), $this.data('state_id'));
    return false;
  });
...
});

You actually don't have to jwrap the clicked object, as you can get the attribute values with either dataset API (if you have the luxury of not supporting IE9-, though) or simple getAttribute method. Yet I found this syntax more clean and readable.

like image 96
raina77ow Avatar answered Feb 24 '26 17:02

raina77ow


The correct way is:

<a href="#" id="myanchor" data-nm="state_name" data-id="state_id">ADD STATE </a>

<script type="text/javascript">
$(document).ready(function(event) {
   event.preventDefault();
   $('#myanchor').on('click', function() {
      openbox_state( $(this).data('nm') , $(this).data('id'));
   });
});
</script>
like image 20
Nelson Avatar answered Feb 24 '26 18:02

Nelson