I have some html and script here:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#options").attr("data-previousvalue1", $("#options").val());
$("#options").data("previousvalue2", $("#options").val());
});
</script>
</head>
<body>
<select id="options">
<option value="1">option 1</option>
<option selected="selected" value="2">option 2</option>
<select>
</body>
</html>
So, this only half works as I expect it to:
$("#options").attr("data-previousvalue1", $("#options").val());
sets data-previousvalue1=2 which is good
but I expected:
("#options").data("previousvalue2", $("#options").val());
to set data-previousvalue2=2.
Do I misunderstand the data method? From my searching and reading this should work.
Here's the output html: