help with simple javascript code

Discussion in 'JavaScript' started by piri00, Nov 27, 2009.

  1. #1
    Hi

    I am new to javascript and I created my form just I have only one problem
    my function count () works with text field outside form tags but when i add those two text fields inside form tags function count() doesnt work any help please?

    My code:

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

    var qua = document.getElementById("quantity").value;

    var cost = 50
    var total = qua*cost
    document.getElementById("count").value = total;
    }
    </script>

    <form>
    <input name="quantity" type="text" id="quantity" onblur="count()" value="0" size="5" maxlength="5" />
    <input name="price" type="text" value="0" size="10" id="count" maxlength="10" readonly="true" />
    </form>
     
    Last edited: Nov 27, 2009
    piri00, Nov 27, 2009 IP
  2. piri00

    piri00 Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Found what was wrong I used id of input Count same as function Count()...
    when i changed id of input to anything else it works.
    Just dont know why it works with same id of input and same name of function when it is not close in form tags... :)
     
    piri00, Nov 27, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    probably because it is trying to access 'count' as an object within the form rather then the global functoin that you want it to access.
     
    camjohnson95, Nov 28, 2009 IP
  4. Amit Oxy

    Amit Oxy Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    duplicate count objects
     
    Amit Oxy, Nov 28, 2009 IP