Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Round Number onchange

Status
Not open for further replies.

bla1979

MIS
Jun 1, 2006
9
US
I have been trying to figure a way to round the value of a number onchange event. Just not working..



<html>
<head>
<script type="text/javascript">
function round(x)
{
var y=document.getElementById(x).value
document.getElementById(x).value=y.Math.round()
}
</script>
</head><body>Enter your name:
<input type="text" id="fname"
onchange="round(this.id)"></body>
</html>
 
I haven't typed it in to see what might work, but have you considered using onkeyup (or onkeypress) rather than onchange, since it is text?

Secondly, and this is a question for me, why do you call the function in this manner: round(this.id);
and then in the function, use the document.getElementById... ?

You could just do: round(this);

function round(x)
{
var y=x.value;
.
.
.

 
What about onBlur, so you know the user has finished his/her changes before you tamper with the input?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top