How can I get the data-value attribute as a string?

Viewed 59
<button type="button" data-value="100" id="btn">BUTTON</button>

const btnVal = $("#btn").data("value");
console.log(typeof btnVal); -- "btnVal" type is number

I want to get the value as a string, but is there any other way to use toString()?

1 Answers

Just read it as an attribute. like this:

const btnVal = $("#btn").attr("data-value");
console.log(typeof btnVal);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<button type="button" data-value="100" id="btn">BUTTON</button>

Related