Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

displaying NoN instead of city name. I have tried following code-

<script type="text/javascript"  language="javascript" >
function submitcity(c){
document.getElementById("ct").setAttribute('value',+c);//displaying NoN
//document.cityform.submit();
alert(c);
}</script>

In Body

<img src="images/reset.jpg" width="80" height="24" onclick="submitcity('bhopal');" />
<form  action="" method="post" name="cityform" style="display:none;">
<input type="hidden" name="city" id="ct" value="" /></form>
share|improve this question
add comment

3 Answers

up vote 5 down vote accepted
+c

Putting a + before a value will attempt to cast it as a number. You're not passing a number. What you're infact seeing is NaN.

Remove the + from behind your variable.

document.getElementById("ct").setAttribute('value',+c);

becomes

document.getElementById("ct").setAttribute('value',c);
share|improve this answer
1  
Thanks its working... –  Ravikant Jul 10 '13 at 15:34
add comment

Remove the unary plus from +c

document.getElementById("ct").setAttribute('value',+c);

You are trying to type cast string (which doesnt represent a number) to numeric value prefixing it with + and hence seeing NaN (Not a Number), it should be:

document.getElementById("ct").setAttribute('value',c);

+ is a unary operator used for type casting to numeric value.

See Unary Plus operator

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. For example, y = +x takes the value of x and assigns that to y; that is, if x were 3, y would get the value 3 and x would retain the value 3; but if x were the string "3", y would also get the value 3. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

Fiddle

share|improve this answer
add comment

Try this,

function submitcity(c){
   document.getElementById("ct").setAttribute('value',c);//remove + sign before c
   //document.cityform.submit();
   alert(c);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.