2

I have been trying to apply solutions I found on both Google and Stackoverflow, but they don't seem to be working.

What exactly is going wrong here? The checkbox is insignificant here and I can take it out, but it makes no difference.

<div class="form-group">
    <label for="tbid" class="col-lg-3 control-label">Results per page </label>
        <div class="col-lg-3">
            <input type="checkbox" class="checkbox" display="inline-block" name="checkbox_results" onclick="checkbox_results_click();">
            <input type="text" id="results" class="form-control" name="results" placeholder="results">
        </div>
</div>

Then in the js portion, I am trying to convert results into an int.

<script type="text/javascript">
    int results = null;
    var x=document.getElementById("results").value;

    results = parseInt(x);
    if(results==null)
        results=10;

    var pager = new Pager(results);

</script>

EDIT: I should also add that if I just put a int parameter when calling pager, like 25, for example, it actually works. So something is going wrong with results.

krikara
  • 2,395
  • 10
  • 37
  • 71
  • Note that [parseInt](http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2) always returns either a number or NaN, neither of which ever `== null`. So the test should be `if (!isNaN(results))`. Also, parseInt should be used with a radix `parseInt(x,10)` – RobG Oct 25 '13 at 03:50
  • Yeah I did x,10 at first, but it didn't seem to make a difference. For whatever reason, it wasn't getting into the pager. I'll change it back now – krikara Oct 25 '13 at 03:53
  • The radix won't fix your issue, it just means *results* will be set to the value you expect. `parseInt('09')` returns `9` in some browsers, `1` in others whereas `parseInt('09',10)` returns `9` everywhere. – RobG Oct 25 '13 at 03:57
  • Possible duplicate of [convert string to a number in javascript](http://stackoverflow.com/questions/11613705/convert-string-to-a-number-in-javascript) – Liam Jul 15 '16 at 14:25

4 Answers4

2

Instead of using parseInt use parseFloat it will work like this

results = parseFloat(x);
soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
0

there's no int in js, you could do:

var results;
var x=document.getElementById("results").value;

results = /^[\d]+$/.test(x);
if(!results)
    results=10;

var pager = new Pager(results);
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Hmm, this `results` gives me this error in google chrome "Script on page used too much memory. Reload to enable scripts again" – krikara Oct 25 '13 at 05:34
0

The trick is +(element.value), add || default_value in case is invalid, or nothing. Or add your own checks.

<!DOCTYPE html>
<html><head><title>to int()</title>
<script type="text/javascript">
function doResults()
{
    var element = document.getElementById('results'),
        value = +(element.value) || 0;

    // write back the value in case is invalid
    element.value = value;

    alert("The value is " + value);
}
</script></head>
<body>
<input type="text" id="results" class="form-control" name="results" placeholder="results">
<button onclick="doResults();">Check Results</button>
</body></html>
aaronps
  • 324
  • 1
  • 6
0

Use parseFloat it will work like this

<script type="text/javascript">
    int results = null;
    var x=document.getElementById("results").value;

    results = parseFloat(x);
    if(results==null)
        results=10;

    var pager = new Pager(results);

</script>