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>

1
  • 1
    Your edit is still incomplete - ".val()" gives a string. Commented Jul 25, 2021 at 11:57

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.