views:

154

answers:

3

I find myself using this method a ton to perform actions based on a anchor tag's URL (with jQuery):

("a").live("click", function(event) 
{
    event.preventDefault();
    var href = $(this).attr("href");
    // do something with URL
}

Elsewhere in my app, I've got some tabular data which I'm adding inline edits to. When you double click a <td> in the <table>, it makes the data editable (text, date select, etc) and hitting "enter" will make an $.ajax request to save the new value. My question is, if each one of these <td>'s has a href associated with it, how/where should I store the uri?

For example, a <td> would have a URL like /articles/field/title/id/5 which I would parse using javascript and send a post using some of the params.

Is this acceptable:

<td href="/articles/field/title/id/5">

And then use the same javascript as above? Or...

  • Should I add a hidden inside the ?
  • Should I wrap the content in an tag inside the and make that double-clickable instead?
  • Or should I use some other property?

Ideas very much welcome.

+3  A: 

href is not a valid attribute of the <td> tag. If you want the contents to be clickable in that manner, wrap the inside in a traditional <a> tag and handle that as you would any other link.

VoteyDisciple
+1  A: 

an option would be something like:

<td id="-articles-field-title-id-5">

in the case of not using IDs, "-" Can be any character.

andres descalzo
Really simple solution, but works perfectly for me! I'll start messing around with the "data" feature of HTML5 later ;)
Typeoneerror
var uri = self.attr("id").replace(/\-/g, "/") + "/value/" + val;
Typeoneerror
yes, that's the idea, replacing the ID character "-" with "/". As commented "@ VoteyDisciple" is not a valid structure to add the attribute "href" to the "tr". But many other plugins use to add additional attribute (not a "href") to work on that element. do not really know how this affects the HTML structure, but put it in the ID makes it valid in HTML.
andres descalzo
A: 

jQuery optionally has a metadata plugin, where attributes are stored in the class attribute:

<td class="myClass { myAttrib: 'attrVal', mySecondAttrib: 69 }">

http://plugins.jquery.com/project/metadata

jeef3