Get Custom Attribute Value In Jquery
i have two span's and where i have a custom attribute. I want to get the value of it by clicking .Here is my html
Solution 1:
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);
});
Solution 2:
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
<spandata-key="<?phpecho$key; ?>"><?phpecho$answer['vote']; ?></span>
jQuery
$(document).ready(function() {
$( ".tup" ).click(function(event) {
event.preventDefault();
var key = $('span').prev().data('key');
alert(key);
});
Solution 3:
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"