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

Time:12-09

<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()?

CodePudding user response:

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