Changing the hidden element's value in javascript

Discussion in 'JavaScript and AJAX' started by rag84dec, Feb 7, 2008.

  1. rag84dec

    rag84dec New Member

    Joined:
    Jul 17, 2007
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    i want to change the hidden elemtn value in the javascript.
    the name of the filed will have to be formed in the javascript.

    like this
    Code:
    var temp="name"+1;
    
    document.[B]temp[/B].value="newname";
    
    i want temp to be replaced by name1 so that the value
     of name1 field shud change.like this "[B]document.name1.value="newvalue" [/B] "
    



    Code:
    
    <input type='hidden' name='name1' value'Val'>
    
     
    Last edited: Feb 7, 2008
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Code:
    var temp="name"+1;
    // change the value now
    document.forms[0].elements[temp].value = 'newvalue';
    
     
  3. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    If you want to change the "value" (as you say) of an element named "name1" presumably you just do this:
    Code:
    name1.value = 'newvalue'
    ...
    <input type="hidden" name="name1" value="oldvalue">
    
    If you are trying to programmatically change from name1 to name2 etc., then you want eval, which allows you to execute a string as code.
    Code:
    <script>
    function changeit() {
       var newval = "xxxxxxxxxxxxx"
       for (var i = 1; i <= 5; ++i)
          eval( 'name' + i + '.innerText="' + newval + '"' )
    }
    </script>
    
    <body>
    <p id=name1>Paragraph 1</p>
    <p id=name2>Paragraph 2</p>
    <p id=name3>Paragraph 3</p>
    <p id=name4>Paragraph 4</p>
    <p id=name5>Paragraph 5</p>
    <button onclick=changeit()>Press</button>
    </body>
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice