0

I want to change value of textbox to -999, if it is having a value of NaN. I have written some code but it is not working, please suggest me what I need to change in this.

if($('input[name="tt10"]').val() === NaN) {
$('input[name="tt10"]').val('-999');
}
<input name='tt10' value='NaN'>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Rahul Dev
  • 71
  • 5

2 Answers2

1

You need to call .val() to get the value of the input. And since the value is a string, you need to compare it with a string.

if($('input[name="tt10"]').val() === 'NaN') {
    $('input[name="tt10"]').val('-999');
};
<input name='tt10' value='NaN'>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You have to use .val() to get value from input field.
Like this:

$('input[name="tt10"]').val()

And with that value now, you can check if value of input is NaN or not:

if($('input[name="tt10"]').val() === 'NaN') {
    $('input[name="tt10"]').val('-999');
};

For more information you can visit official documentation.

imxitiz
  • 3,920
  • 3
  • 9
  • 33