I have a list of stories on a page and each has several jQuery buttons for different actions. When the button is clicked, I need to know what story it is was clicked for.
Currently I have each story wrapped in a div
with an id of format story_<story_id>
and then in the handler I traverse up and parse out the id.
<div id="story_1" class="story">
Blah blah blah
<input type="button" class="report-button" value="Report">
</div>
<div id="story_2" class="story">
Blah blah blah
<input type="button" class="report-button" value="Report">
</div>
<!-- more stories -->
<div id="story_9" class="story">
Blah blah blah
<input type="button" class="report-button" value="Report">
</div>
<script type="text/javascript">
$(function() {
$('.report-button')
.button()
.click(function() {
var id_str = $(this).parents(".story").attr("id");
var id = id_str.slice(6)
alert(id);
});
});
</script>
This works, but I wonder if there is a downside to this technique or if there is a better way.
I have also thought of:
- Using a form for each story with a hidden input with the id
- Putting the story id in the button id and parsing that, etc.
id="report_story_123"
Is there a standard way to handle this?