Passing arguments and values from HTML to jQuery (events)

Posted by Jaroslav Moravec on Stack Overflow See other posts from Stack Overflow or by Jaroslav Moravec
Published on 2010-05-25T13:31:13Z Indexed on 2010/05/25 23:31 UTC
Read the original article Hit count: 277

Filed under:
|
|
|
|

What is the practice to pass arguments from HTML to jQuery events function. For example getting id of row from db:

<tr class="jq_killMe" id="thisItemId-id">
 ...
</tr>

and jQuery:

$(".jq_killMe").click(function () {
     var tmp = $(this).attr('id).split("-");
     var id = tmp[0]
     // ...
}

What's the best practise, if I want to pass more than one argument? Is it better not to use jQuery? For example:

<tr onclick="killMe('id')">
 ...
</tr>

I didn't find the answer on my question, I will be glad even for links. Thanks.

Edit (pre solution)

So you suggested two methods to do that:

  1. Add custom attributes to element (XHTML)
  2. Use attribute ID and parse it by regex
  3. Attribute data-* attributes in HTML5
  4. Use hidden children elements

I like first solution, but... I would like to (I have to (employer)) produce valid code. Here is a nice question and answers:
http://stackoverflow.com/questions/994856/so-what-if-custom-html-attributes-arent-valid-xhtml

And the second is not so pretty as the first, but valid. So the compromise is...

The third is the solution for future, but here is a lot of CMS where we have to use XHTML or HTML4. (And HTML5 is the long process)

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html