1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Changing the hidden element's value in javascript

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

  1. rag84dec

    rag84dec New Member

    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

    Code:
    var temp="name"+1;
    // change the value now
    document.forms[0].elements[temp].value = 'newvalue';
    
     
  3. oogabooga

    oogabooga New Member

    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