Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 888
eastglow Avatar asked Jul 31 '26 12:07

eastglow


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>
like image 174
Saeed Shamloo Avatar answered Aug 03 '26 02:08

Saeed Shamloo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!