what js event?

Discussion in 'JavaScript' started by promotingspace.net, Sep 3, 2008.

  1. #1
    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:
    function devideby100(number){
       var obj
       obj = document.getElementById('myfield')
    obj.value='number/100'
    }
    Code (markup):
    then my fields will be:
    <input type="text" value="" OnEvent='divideby100();'
    Code (markup):
    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
     
    promotingspace.net, Sep 3, 2008 IP
  2. scriptdance

    scriptdance Active Member

    Messages:
    340
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I think you should use onBlur="divideby100();".
     
    scriptdance, Sep 3, 2008 IP
  3. Autodidact

    Autodidact Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If I understand your request this is what you need

    function divideby100(obj) {
        var input = obj.value;
        if (input.length > 0 && !isNaN(input)) {
          obj.value = input/100;
        } else {
           obj.value = '';
        }
    }
    Code (markup):
    <input type="text" onblur="divideby100(this)" />
    Code (markup):
    This protects you when the user types in something other than a number
     
    Autodidact, Sep 5, 2008 IP