I'm working on a JavaScript calculator and I'm refactoring the code to DRY it out by using the HTML "data" attribute. (I want to have one function for data entry, not ten functions--one per digit--as I had in v1.0.) My goal is to use jQuery to parse an integer from the data attribute, but I've hit a wall.
Here is a row of buttons in HTML:
<div class="row" id ="row3">
<div data-number=“7” class="button number gray" id="seven">7
</div>
<div data-number=“8” class="button number gray" id="eight">8
</div>
<div data-number=“9” class="button number gray" id="nine">9
</div>
<div data-operand="*" class="button orange operand" id="multiply">×
</div>
</div>
Here's an event trigger:
$(".number").mouseup(pushNumber);
And here's the broken function:
var pushNumber = function() {
var num = $(this).data("number");
var parseNum = parseInt(num);
console.log(parseNum);
console.log(typeof num);
}
When I click a given button, console.log(parseNum) returns "NaN", and console.log(typeof num) returns "string". Based on the documentation for jQuery, I believe that .data should return a number here. So, all that said...
1. How can I make sure that .data returns a number, not a string?
2. Why can't I use the parseInt method on num?
Here's the full project:
https://codepen.io/halfalpine/pen/MeJwmV?editors=1010
I've searched for something relevant to the best of my ability but I've come up short. My apologies if this entry is redundant!
data-number=“9”. Actually, that may be the whole problem.