tags:

views:

106

answers:

8

Say I have a table of items and I want to add to every row a button to delete that item. Items come from a database, so they have an unique ID. In the jQuery function I have to retrieve the ID of said item so that I can open a confirm box and eventually redirect the browser to the delete page (nevermind security checks, it's for internal use).

Where in the markup is it better to place the ID?

+5  A: 

I think it's better to place an id as a part of row's id:

....
<tr id="item_3">...<td>delete tag</td></tr>
<tr id="item_4">...<td>delete tag</td></tr>
<tr id="item_5">...<td>delete tag</td></tr>
....

And then in jQuery get an id using simple regex.

Update: Or, just put it into rel attribute of a delete button/anchor.

Update 2: Since regex may slow down jQuery performance in this case, try to put id's as rel attribute of each row.

Darmen
What selector would I use in this case to identify only the `delete` cells? Give them a class then fetch the ID from their parent?
kemp
@kemp Yes, but you should fetch an `id` from parent of parent :)
Darmen
`rel` is an interesting solution, any value is valid inside it?
kemp
@kemp, not sure about `rel` (http://www.w3schools.com/TAGS/att_a_rel.asp) valid values, you should check it with validator
Darmen
+5  A: 

Give each item a unique ID. The '#' selector works fastest, because it maps to a native javascript method.

me_here
Just an addition: the ID must begin with a letter. More info at http://www.w3.org/TR/html401/types.html#type-name
Darmen
well I can give them a unique ID, but I can't use the `#` selector, unless I want to write a function for every item. I would have to access them as descendant of - say - their containing table. Is this right?
kemp
You don't need to use an ID selector, you can use `$('button').closest('tr')` to select the parent row.
Rowan
+2  A: 

It would make sense to put it into the <tr> element, because that is the item that holds all the data/markup related to this item. If you know in advance that the delete button is the only element which will interact with the unique ID, it would be slightly faster and easier to just add it to the button itself.

Jasper De Bruijn
A: 

I would rather use div layout instead of table

Drakiula
-1 for not grepping the question. This is clearly a semantically valid use case for tables.
macke
Why so? Can't we use tables even for data coming from database tables anymore?
kemp
The question is how to find best suitable place for items identification, it's not about what kind of markup tags should be used in this situation.
Darmen
+1  A: 

You should put it on a form surrounding the delete button:

<tr>...<td><form action="delete.htm?id=001"><input type="submit">Delete</input></form></td></tr>

I realise that this might seem a little heavyweight, but this works without javascript, which is always best-practice.

graphicdivine
Good point with the non js support, could be enhanced with a confirmation modal and Ajax.
Rowan
+2  A: 

There are a couple of ways you could achieve this.

Firstly, try the jQuery meta plugin. This allows you to embed JSON data in the class attribute of an element. It validates and doesn't affect the classes applied to the element. Your tr could look like this:

<tr class="some-class {uid:'12345', asMuchDataAsYouLike}">

Alternatively (if you don't mind using HTML5), use a data attribute (here, here):

<tr data-uid="12345">

Note: This will validate as HTML5 if you use the correct DocType (<!DOCTYPE html>).

Edit (example usage):

HTML:

<tr data-uid="12345">
    <td>Some Data</td>
    <td><button class="delete">Delete</button></td>
</tr>

Javascript:

$('button.delete').click(function(){
    // obviously you'd do something different with the data ;)
    alert($(this).closest('tr').attr('data-uid'));
    });
Rowan
I think you meant this plugin: http://docs.jquery.com/Plugins/Metadata/metadata
RenderIn
Oops, thank you
Rowan
A bit overkill for this specific situation, but very interesting alternative solutions, thanks
kemp
A: 

Not tested, but assuming you are using jQuery 1.3+

$(this).closest('tr').attr('id');

Should give you the id of the row that you have clicked on (noting that you'll have to give the row an id first when you build the table.

Blair McMillan
+3  A: 

Is it just me, or is it that simple?

<tr id="cell-8958"><td>..<td><td class="delete">Delete me</td></tr>

...

$('.delete').click(function(){
  thisid = $(this).parent().attr('id');
  dostuff();
});

Edit: Or, if you want to begin with numbers:

.id { display: none; }

...

<tr><td class="id">cell-8958</td><td>..<td><td class="delete">Delete me</td></tr>

...

$('.delete').click(function(){
  thisid = $(this).parent().find('td.id').text();
  dostuff();
});

P.S.: CSS might be different, haven't tested in browsers.

A.

Adam Kiss
Not SEO-friendly though =)
Darmen
"nevermind security checks, it's for internal use" - read *carefully* ;] :D
Adam Kiss