document object

Discussion in 'JavaScript' started by junerin, Sep 3, 2007.

  1. #1
    **working version**
    function update(box){

    if(box==1){
    document.nextform.box1.value++
    }

    if(box==2){
    document.nextform.box2.value++
    }


    }

    **prevferred version**
    function update(box){
    var a1 = "document.nextform.box"
    var a2 = ".value"

    a1+box+a2 //this is the problem line


    }

    What I am trying to accomplish is the removal of the IF's statements.
    Any suggestions?

    thanks
     
    junerin, Sep 3, 2007 IP
  2. mjamesb

    mjamesb Member

    Messages:
    88
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    48
    #2
    I am not sure what you are trying to do in your example? Are you trying to dynamically generate the name of the box?

    if this is what you are trying to do to get rid of the if statement you need to use an eval statement to evaluate the string as an object.

    var myval = eval(a1+box+a2)
     
    mjamesb, Sep 3, 2007 IP
  3. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Me neither, but if you allow me one suggestion prefer: document.getElementById('box1').value to document.nextform.box1.value

    that way you can code differently using: update(i) where "i" could be 1 or 2 in your example and using the generic: document.getElementById('box'+i.toString()).value.

    Does it solve the problem?
     
    webrickco, Sep 4, 2007 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Make it clear that requires the element to have an id, even thought I.E. would allow it.
    toString isn't needed there.
     
    Logic Ali, Sep 4, 2007 IP
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Of course it requires an id...
    And the toString insures standard compatibility between same types of variables. It's good pratice only.
     
    webrickco, Sep 4, 2007 IP
  6. junerin

    junerin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have attached a working example. I am trying to get rid of repeating IF statements but I don't know if there is a way or if it's possible

    document.nextform.box1.value++

    what I am trying to do with the code above is that number 1 to be changing and the value returned to the form.

    I hope I am making myself clearer.

    thanks again
    I will try your suggestions and see if it works
     

    Attached Files:

    junerin, Sep 4, 2007 IP
  7. junerin

    junerin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thanks for all your help

    I used eval(a3) and it worked
    I also corrected
    var a2 = ".value

    to
    var a2 = ".value++
     
    junerin, Sep 4, 2007 IP