Lynchie
Lynchie

Reputation: 1149

Variable Returning NaN Value - Cannot find out why

I am having to pickup from where someone in the business left off many years ago with an aging texting system.

It was built using ASP classic and sends a string to an API that then texts out, all this is neither here nor there. The problem i have is no JS experience, I am am a SQL Developer and did a little bit of ASP Classic (VBScript) years ago.

This piece of JScript picks up information from several form boxes and then places them in a string which is then passed to variable on a processing page to text out. The fields 'QValue, Indemnity and Excess' are all numeric. The Cover is text and it is replacing the cover text with 'NaN' now I understand this is for 'Not A Number' well that is exactly what it is, not a number but I want the text string.

Here is the snippet of code in question:

<script type="text/javascript">
function changeMessageText()
{

var messagetxt = document.getElementById('message').value

var QValue = document.getElementById('QValue').value
var Cover = document.getElementById('Cover').value
var Excess = document.getElementById('Excess').value
var Indem = document.getElementById('Indemnity').value

var messagetxt=messagetxt.replace("[QValue]", + QValue)
var messagetxt=messagetxt.replace("[Cover]", + Cover2)
var messagetxt=messagetxt.replace("[Excess]", + Excess)
var messagetxt=messagetxt.replace("[Indem]", + Indem)

document.getElementById('messageText').innerHTML = messagetxt;

}
</script>

Cheers.

Upvotes: 1

Views: 551

Answers (3)

Ezhil V
Ezhil V

Reputation: 882

When you do string.replace(searchvalue,newvalue), there is no need of + before the newValue

var messagetxt=messagetxt.replace("[QValue]", QValue)
//cover or cover2 whichever appropriate
var messagetxt=messagetxt.replace("[Cover]",  Cover) 

var messagetxt=messagetxt.replace("[Excess]",  Excess)
var messagetxt=messagetxt.replace("[Indem]",  Indem)

Upvotes: 5

Mark Topper
Mark Topper

Reputation: 210

Here is how you do it:

var messagetxt = document.getElementById('message').value;

var QValue = document.getElementById('QValue').value
var Cover = document.getElementById('Cover').value

var messagetxt=messagetxt.replace("[QValue]", QValue)
var messagetxt=messagetxt.replace("[Cover]", Cover)

document.getElementById('messagetxt').innerHTML = messagetxt;

Here is a working example of this: http://jsfiddle.net/F24cr/

Enjoy

Upvotes: 0

Quentin Proust
Quentin Proust

Reputation: 108

Is it normal that you use Cover2 in the replace where you read the input value and store it in the Cover variable ?

Those are two different variables and from the code you provided, we can only assume that Cover2 is initialized with NaN (which might not be the case, it can be copy/paste error).

Upvotes: 1

Related Questions