views:

70

answers:

5

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)

A: 

It depends on context. Express the data in the most semantically way that is appropriate.

It might use a class. It might use data in a cell within the row. It might use data in an attribute of an element in a cell (e.g. a URI in the href of an anchor).

There isn't a generic solution that is always best.

David Dorward
+1  A: 

The simplest way to do this is to put the information into custom attributes, like this:

<tr class="jq_killMe" id="thisItemId-id" itemId="42" itemName="Answer">
 ...
</tr>

$(".jq_killMe").click(function () {
     var id = $(this).attr('itemId');
     var name = $(this).attr('itemName');
     // ...
}

Remember to correctly escape the values, to prevent XSS.

SLaks
you should mention that custom attributes are not neccessarily crossbrowser compatible, nor does it work with standard strict dtd
jAndy
@jAndy: AFAIK, this will work in every browser.
SLaks
@SLaks: it may be supported by all browsers nowadays, as long as xhtml is supported and available. Anyways, I'm not quite sure about W3C and custom attributes.
jAndy
XHTML isn't supported or available in IE < 9. (Unless you mean tag soup with an XHTML Doctype)
David Dorward
+1  A: 

You might choose between different ways depending on how many parameters you might need. If you just need more than one, you can either add custom attributes, but that might not be html compliant.

Sometimes you might even need to embed the values within the id like you are doing:

But I'd do this like this:

where 1 is your id, and then use RegEx instead of doing the split: $(".jq_killMe").click(function () { var fullid= $(this).attr('id); // do Regex here to either replace or match anything that follows 'thisItemId' }

Damiano
+1  A: 

And what about nested elements?

<style type="text/css">
  .data {display: none}
</style>

<tr class="jq_killMe">
   <span id="data-id" class="data">my id</span>
   <span id="data-name" class="data">answer</span>
   ...
</tr>

But there is some questions:

  • Is it this way safe as to accessibility?
  • How percentage of users has disabled css?

Edit

Better way is to use <input type="hidden" />

<tr class="jq_killMe">
   <input type="hidden" name="data-id" value="my id" />
   <input type="hidden" name="data-name" value="answer" />
   ...
</tr>

and jQuery seems like this

$(".jq_killMe").click(function() {
    var id = $(this).children("input[name='data-id']");
    //...
});
Jaroslav Moravec
A: 

Another option is to provide the data via script and then use the row-id to look it up.

e.g.

<script>
 var MyData = {
  table: [
   { name: "John", age: 20 },
   { name: "Jane", age: 40 }
  ]
 }
</script>

then lookup the data with the following:

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