Somk Somk - 1 month ago 5
CSS Question

jQuery data() returns undefined constantly

Below is the HTML and following jQuery. If I return the attr("data-conn"); it alerts the correct value but using .data("conn") it doesn't. Why?

<span>
<!-- ... -->
<a href="link.html" class="textbutton" data-conn="text3">
<img src="/images/image.png">
</a>
</span>
<span style="width:610px;height:200px;float:right; background-color:#bcbcbc;font-size:15px;line-height:15px;">
<div class="texts" id="text1">Initial Header</div>
<span class="texts" id="text2" style="display:none;">Text for another one</span>
<span class="texts" id="text3" style="display:none;">Content Text</span>
</span>

<script type="text/javascript">
$(".textbutton").click(function(){
var link = $(this).data("conn");
alert(link);
$(".texts").fadeOut(1000, function() {
$("#text2").fadeIn(1000);
});
return false;
});
</script>

Answer

the way your trying works fine

var link = $(this).data("conn");

use latest version jquery . Here is a working demo

you may alternatively try like this

var link = $(this).attr("data-conn");