Is this right way to convert data attributes values to number in javascipt? Need to get valid numeric value or 0

Joined
Oct 24, 2013
Messages
43
Reaction score
0
I am using the following code to convert data attribute values to Numbers. I want to get a valid numeric value or 0 in all cases. e.g if the data attribute value is blank("") then I need 0 in that case.

I am using the following code to convert data attribute values to Numbers. I want to get a valid numeric value or 0 in all cases. e.g if the data attribute value is blank("") then I need 0 in that case. The intention is to avoid getting NaN because I am using the result in calculations.

JavaScript:
Number($("td[id^='tdCumulativeUnderOverSpendPrevious']").data("val") || 0);
 
Last edited:
Joined
Jul 3, 2022
Messages
93
Reaction score
23
It seems that your right-hand parenthesis of Number is put at the wrong position:

JavaScript:
Number( $("td[id^='tdCumulativeUnderOverSpendPrevious']").data("val") ) || 0;

You can inspect what is returned by this logic, for exapmle, this way:

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style>
  
    </style>
  </head>
  <body> 
      <table>
    <tbody>
    <tr>
    <td id="tdCumulativeUnderOverSpendPrevious_1" data-val="Infinity">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_2" data-val="">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_3" data-val=" ">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_4" data-val="qwe123">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_5" data-val="123">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_6" data-val="false">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_7" data-val="null">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_8" data-val="0,2">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_9" data-val="0.2">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_10" data-val="0.2 * 5">12345</td>
    </tr>
    </tbody>
    </table>
    <script>
    const arr = [...document.querySelectorAll('[id^=tdCumulativeUnderOverSpendPrevious]')].map( n => Number(n.dataset['val']) || 5 );
    console.log( arr ); // [Infinity, 5, 5, 5, 123, 5, 5, 5, 0.2, 5]
    </script>
  </body>
</html>
 
Last edited:
Joined
Oct 24, 2013
Messages
43
Reaction score
0
It seems that your right-hand parenthesis of Number is put at the wrong position:

JavaScript:
Number( $("td[id^='tdCumulativeUnderOverSpendPrevious']").data("val") ) || 0;

You can inspect what is returned by this logic, for exapmle, this way:

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title></title>
    <style>
 
    </style>
  </head>
  <body>
      <table>
    <tbody>
    <tr>
    <td id="tdCumulativeUnderOverSpendPrevious_1" data-val="Infinity">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_2" data-val="">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_3" data-val=" ">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_4" data-val="qwe123">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_5" data-val="123">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_6" data-val="false">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_7" data-val="null">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_8" data-val="0,2">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_9" data-val="0.2">12345</td>
    <td id="tdCumulativeUnderOverSpendPrevious_10" data-val="0.2 * 5">12345</td>
    </tr>
    </tbody>
    </table>
    <script>
    const arr = [...document.querySelectorAll('[id^=tdCumulativeUnderOverSpendPrevious]')].map( n => Number(n.dataset['val']) || 5 );
    console.log( arr ); // [Infinity, 5, 5, 5, 123, 5, 5, 5, 0.2, 5]
    </script>
  </body>
</html>
Thanks a lot. Appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top