Skip to content Skip to sidebar Skip to footer

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:

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"