SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question feeding functions

    this code isnt working yet. The function should check if a dynamically feed input is checked, and if it is, then add some value to a field, or take it from it.

    Code:
    function sumar(checkid,suma){
          if (document.form1.checkid.checked) {
                document.getElementById("var_input1000").value = parseFloat(document.getElementById("var_input1000").value) + parseFloat(suma);
          }else{
                document.getElementById("var_input1000").value = parseFloat(document.getElementById("var_input1000").value) - parseFloat(suma);
          }
          document.getElementById("unitario1000").textContent = parseFloat(document.getElementById("var_input1000").value).toFixed(2);
    }
    Firefox says:
    Error: document.form1.checkid has no properties

    The function caller is :
    Code:
    <input id="check73" name="check73" onclick="javascript:sumar(check73,1.20)" type="checkbox">

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use,
    Code:
    if (document.getElementById(checkid).checked) {
    Birnam wood is come to Dunsinane

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Or if you want to keep a similar syntax to the original code

    Code javascript:
    if (document.form1[checkid].checked) {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still having trouble.

    Remember that im parsing the function with the first variable containing the field's id & name. The problem; in the function, it searches for the variable's name, not it's content.

    It searches by checkid, while it should use "checkid"'s value, that is, in this example: check73.



  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to enclose the ID in quotes in the function call (and lose the 'javascript:' bit from the event attribute – it's not a URI):

    Code:
    <input id="check73" name="check73" onclick="sumar('check73',1.20)" type="checkbox">
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got point!

    Thanks for your help indeed!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •