2

I'd like to get a census on methods of removing/adding records via AJAX and updating the front-end.

For tabular data (take an inbox for example):

enter image description here

When I want to remove that first message, where/how should I be referencing the ID of that message and sending it to my AJAX call? I've seen some people put the ID in a hidden field, or use the checkbox id attribute...

How is this transaction properly handled so that when my call is successful, I can "remove" that row with jQuery?

dcolumbus
  • 9,596
  • 26
  • 100
  • 165

1 Answers1

1

What I typically use to "attach" data like this to HTML elements is to use the HTML5 data attribute. This will allow you to store multiple pieces of data for use w/ Javascript/jQuery/Ajax, without doing anything "hacky" like embedding stuff in IDs or having to parse out values.

For example, in your case of a table row, you could have something like this:

<tr data-email-id="123"><td>...</td></tr>

Then it would be simple to reference in your jQuery (assume $(this) references the tr):

var emailId = $(this).attr('data-email-id');
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
  • So you're suggesting just making up an attribute, correct? I believe I've done this before... and from what I understand, jQuery doesn't care and it doesn't effect the integrity of the page. – dcolumbus Jun 17 '11 at 01:17
  • I don't believe the browser cares that you're using HTML5, since this use of HTML5 isn't actually functional -- it's just a means of embedding data. In other words, it won't break anything. It just won't be considered "standards compliant" with that browser. – Jerad Rose Jun 17 '11 at 01:20
  • Quick follow-up, check out [this SO answer](http://stackoverflow.com/questions/3957867/is-there-any-problem-with-using-html5s-data-attributes-for-older-browsers/3957879#3957879) that answers your question re: HTML5 `data` attribute in older browsers (basically says the same thing I just said). – Jerad Rose Jun 17 '11 at 01:22
  • Yeah, this is the clarification I needed: "Internet Explorer and Microsoft has added several custom attributes that are not valid HTML4." - This is very true. I mean, you could create an attribute called "bostonbruinssuck=1" and it will work. Thanks a lot, Jerad! – dcolumbus Jun 17 '11 at 01:26