Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading