I'm encountering an issue with my code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script src="jquery.js"></script>
<script>
$(function(){
$(".classy").data("read","hi");//Not working .working is$("div").attr("data-read","hi");
});</script>
<style>
.classy[data-read='hello']{background:#000;}.classy[data-read='hi']{background:#fff;}</style>
</head>
<body>
<div class="classy" data-read="hello">a</div>
</body>
</html>
After the document is ready, the data-read attribute of .classy changes to hi. However, when I have a CSS style design for .classy[data-read='hi']
, it does not work when using the .data()
method to manipulate the data-read attribute. It only works when I use the .attr()
method instead. Why is this happening?