You are most likley getting undefined because you are not selecting the wanted span, use .prev() to select it
$( ".tup" ).click(function(event) {
event.preventDefault();
var key = $(this).prev().attr('data');
alert(key);
});
you need to use data-key
(or another value, data-id, data-attr....) and retrieve it in jQuery with .data(key)
You need to get the previous element with prev()
try this:
HTML
<span data-key="<?php echo $key; ?>"><?php echo $answer['vote']; ?></span>
jQuery
$(document).ready(function() {
$( ".tup" ).click(function(event) {
event.preventDefault();
var key = $('span').prev().data('key');
alert(key);
});
Storing data in this way is not valid.
You can use html5 data-* propery for custom attribute, for example:
<!DOCTYPE html>
...
<span data-key="some data">value</span>
<script>
var key = $("span").data("key");
</script>
Post a Comment for "Get Custom Attribute Value In Jquery"