Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert input text to int in JavaScript

I am trying to sort a table in HTML by clicking the table header. The sorting is fine for strings, but when comparing numbers, it evaluates "10" < "2". Obviously, I'm comparing strings, but I can't get a number out of my table cell. Here's a working example of what's going on:

 <script type="text/javascript" >
      function sortBy() {
          var table = document.getElementById("table");
          var rows = table.getElementsByTagName("tr");
          var number = rows[1].getElementsByTagName("td")[0].innerHTML;
          document.write(number);  // This should be printing "7".
      }
 </script>

 <?php 
      echo "<table id='table'>
         <thead><tr><th onclick='sortBy()'>CLICK ME</th></thead></tr>
         <tbody><td><input readonly value='7'></td></tbody>";
 ?>

In this example, I'm trying to get the integer 7. I've tried parseInt(number), but that evaluates to NaN, so I tried writing what number was and it prints a textfield instead of the textfield's content. I've checked this by printing number.length, and I get 29, rather than 1.

I've tried number.value but that's undefined.

I've tried replacing .innerHTML with .value, but that's undefined too.

Is there some way I can convert this into a number? Or at least strip away the html tags and such? I realize I can rig up my own compare logic using the string and it's length ( if (A.length > B.length) { A's bigger } else { compare like normal } ), but that's messy and won't help me to understand this problem.

like image 717
Lord Farquaad Avatar asked Jun 16 '17 21:06

Lord Farquaad


People also ask

How do I convert a string to an int in JavaScript?

To convert a string to an integer parseInt() function is used in javascript. parseInt() function returns Nan( not a number) when the string doesn't contain number. If a string with a number is sent then only that number will be returned as the output.

How do I convert a string to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System. Convert class. It's slightly more efficient and straightforward to call a TryParse method (for example, int.

How do you input integers in JavaScript?

JavaScript parseInt() Function The parseInt() function is used to accept the string ,radix parameter and convert it into an integer.


4 Answers

The problem is that you aren't picking the correct tag; you need to add a selector for the <input> that holds the value.

function sortBy() {
    var table = document.getElementById("table");
    var rows = table.getElementsByTagName("tr");
    var number = rows[1].getElementsByTagName("td")[0].getElementsByTagName("input")[0].getAttribute("value");
    document.write(number);  // This should be printing "7".
}
like image 144
RompePC Avatar answered Oct 24 '22 02:10

RompePC


Use Element.getAttribute and parseInt in order to retrieve and parse value correctly:

var number = rows[1].getElementsByTagName("td")[0].getAttribute('value')
document.write(parseInt(number));  
like image 42
Andriy Ivaneyko Avatar answered Oct 24 '22 02:10

Andriy Ivaneyko


You could try using the Number() function to create a new Number wrapper class with the value from the string. This should continue to work even if the value is actually a number.

 <script type="text/javascript" >
      function sortBy() {
          var table = document.getElementById("table");
          var rows = table.getElementsByTagName("tr");
          var number = rows[1].getElementsByTagName("td")[0].innerHTML;
          document.write(Number(number));  // This should be printing "7".
      }
 </script>

 <?php 
      echo "<table id='table'>
         <thead><tr><th onclick='sortBy()'>CLICK ME</th></thead></tr>
         <tbody><td><input readonly value='7'></td></tbody>";
 ?>

Read more in the MDN documentation page.

like image 1
thornjad Avatar answered Oct 24 '22 03:10

thornjad


Aside that the table structure need some corrections, by using innerHTML function you will output everything that td tag contains.
To correct JavaScript code I would suggest to use query selector as below:

function sortBy() {
  var table = document.getElementById("table");
  var rows = table.getElementsByTagName("tr");
  var number = rows[1].querySelector("td > input").value;
  console.log(number);
}
<table id='table'>
  <thead>
    <tr>
      <th onclick='sortBy()'>CLICK ME</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input readonly value='7'></td>
    </tr>
  </tbody>
</table>
like image 1
MaxZoom Avatar answered Oct 24 '22 03:10

MaxZoom