-1

I have a dropdown list and I'm passing the value to a function which performs a task when the value changes.

<li style="background-color:white; float:right">
  <select id="dpItems" onchange='loadScript();'>
    <option value="">Select</option>
    <option value="5">5</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option> 
  </select> items per page
</li>

And the javascript that is getting the value as follows:

function loadScript() {
  var newItems = parseInt($("#dpItems").val());
  alert('New value: ' + newItems);
  console.log('New value: ' + newItems);
}

But I'm always getting NaN value instead of the correct value on the option within the select. I tried changing the value on the option to 5 (without quotes) but same result.

Any ideas? Thanks in advance for any help.

Carlos M
  • 27
  • 7

2 Answers2

2

You can pass its value to the onchange function and it will not be undefined:

<select id="dpItems" onchange='loadScript(this.value);'>
Ramenous
  • 218
  • 1
  • 8
0

You're trying to convert undefined into an integer.

Number.isNaN(parseInt(undefined)) === true;
Reactgular
  • 52,335
  • 19
  • 158
  • 208