JavaScript and client side scripting.
Moderator: General Moderators
m2babaey
Forum Contributor
Posts: 364 Joined: Sun May 20, 2007 9:26 am
Post
by m2babaey » Wed Sep 03, 2008 8:05 am
Hi
I have a form ( with 4 fields for numbers) and want a javascript function to divide the field input by 100 when we go to next field
first i need to know what event should be used
the function should be simple but i don't know how to set the argument
like:
Code: Select all
function devideby100(number){
var obj
obj = document.getElementById('myfield')
obj.value='number/100'
}
then my fields will be:
Code: Select all
<input type="text" value="" OnEvent='divideby100();'
But I'm sure it won't work. this is my first function in javascript. Please let me know the correct code
Thanks in advance
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Wed Sep 03, 2008 1:45 pm
the javascript event you are looking for is most likely to be "onBlur". This event is triggered when focus leaves the object in question.
m2babaey
Forum Contributor
Posts: 364 Joined: Sun May 20, 2007 9:26 am
Post
by m2babaey » Wed Sep 03, 2008 3:12 pm
another not working suggestion is:
Code: Select all
<script type='text/javascript'>
num=1;
function devideby100(){
var obj,number;
obj = document.getElementById('num');
number=parseFloat(obj.value);
if(isNaN(number))
alert('you must enter a number');
else
obj.value=number/100;
num++;
}
</script>
<br><br>
and the tag input:
<br><br>
<input type="text" value="" id="1" onchange="devideby100()">
<input type="text" value="" id="2" onchange="devideby100()">
<input type="text" value="" id="3" onchange="devideby100()">
but i don't know why it's not working ( also tried onblur )
andyhoneycutt
Forum Contributor
Posts: 468 Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls
Post
by andyhoneycutt » Wed Sep 03, 2008 3:21 pm
Javascript:
Code: Select all
<script type='text/javascript'>
function divideBy100(obj)
{
var number = obj.value;
number = parseFloat(number);
if( number > 0 )
{
obj.value = (number / 100);
}
}
</script>
HTML:
Code: Select all
<input type="text" value="" id="1" onblur="divideBy100(this)">
-Andy