Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to identify an element for jQuery interaction

Tags:

html

jquery

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?

like image 258
Matteo Riva Avatar asked Dec 17 '22 03:12

Matteo Riva


1 Answers

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.

like image 87
Darmen Amanbayev Avatar answered Jan 07 '23 07:01

Darmen Amanbayev