So I really prefer using unobtrusive javascript to inline javascript. I find it much easier to work with.
The problem that I keep running into, is that I don't know how to get data for each specific element that I want to work with.
For example:
I have a list, and I generate the following HTML (this is pseudo-code here)
<%for e in list%>
<a href="#" class="delete"><%=e%></a> <!-- <%= e.id %> ??-->
<%end%>
So I want to attach javascript to each of these <a> tags that will invoke an ajax post, but I need the "id" to delete the correct element from the back end.
$('a.delete').click(function() {
// Ajax request using "e.id"
});
Some things that I have tried:
attr
method to send the ajax request to that url. (I don't really like this for POST requests, but it does work.rel
attribute to store the data I need.data
attributes. (I don't know about the availability of this in all browsers, and since we need to support IE6+ I don't really like the idea using this.)What are the best practices for achieving this?
Use the id
attribute to store a string w/ the id like so:
<%for e in list%>
<a id="link_<%= e.id %>" href="#" class="delete"><%=e%></a>
<%end%>
And the read the id by splitting on something like an underscore:
$('#list a').click(function(){
//old var id = $(this).attr('id').split('_')[1];
var id = this.id.split('_')[1]; //thx @mu
$.ajax({
url: 'http://www.someurl.com/delete.aspx?id=' + id,
//...more
})
});
I often store this kind of data in the tag like so:
<div id="list">
<a href="/link" post_id="68">Some post</a>
</div>
Then in jQuery do something like:
$('#list').delegate('a.delete', 'click', function(event) {
event.preventDefault(); // Stop default click event
// ajax stuff here
// $(this).attr('post_id') = 68
});
Update: The .attr('post_id')
doesn't work in IE6.
All of these use the above delegate function.
<div id="list">
<a href="/link"><span class="hide post_id">66</span>Some post</a>
</div>
$('span.post_id', this).text()
Probably one of the worser ideas for just simple ids, but if you need to store complex data then adding it inside an element could be an option.
<div id="list">
<a href="/link/66">Some post</a>
</div>
$(this).attr('href').match(/\/([0-9]+)$/)[1]
This method is obviously dependant on your url. This is one of the most unobtrusive methods, assuming your url structure isn't too complex or change often.
<div id="list">
<a href="/link" id="post_66">Some post</a>
</div>
$(this).attr('id').split('_')[1]
Benefit to this method is you can also style elements based on specific IDs -- sometimes useful. It also makes the most sense. Your ID is unique. That anchor link relates to that post, that unique ID, so using the id attribute makes sense.
You can attach an ID to each <a>
element like this <a href=".." id="delete_<%e.id%>" >
then when you attach event to this link you can find ID via string indexOf and use related ajax function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With