31 users online (1 members and 30 guests)  


  Results 1 to 4 of 4

Related

  1. Replies: 3
  2. Replies: 1
  3. Replies: 1
  4. Replies: 10
  1. #1
    J-Man's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Nov 2003
    Posts
    32

    Assign Value to Hidden Control...on the fly?

    Is there a way to assign a value to a Hidden control on the fly? That is, I want to assign a value to a hidden input type after I receive the value later. OR...is there some other way that I can assign a value to something that can be submitted when the form is submitted? Thanks all!!!!!

  2. #2
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Hi,

    You can assign a value to a hidden form element using JavaScript, exactly the same way you would do it for any other input type.

    heres a simple example with then values alerted via javascript so you can see whats going on -
    Code:
    <form name="badger">
    <input type="hidden" name="stoat" value="greebo">
    </form>
    
    <a href="#" onClick="document.badger.stoat.value='HUNGRY';alert(document.badger.stoat.value); return false;">EEEEK</a>
    
    
    
    
    
    <script language="JavaScript">
    alert(document.badger.stoat.value);
    </script>
    badger and stoat are just the names I used for the form and input, in case your wondering
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  3. #3
    J-Man's Avatar
    Junior Member

    Status
    Offline
    Join Date
    Nov 2003
    Posts
    32
    Quote Originally Posted by QuietDean
    Hi,

    You can assign a value to a hidden form element using JavaScript, exactly the same way you would do it for any other input type.

    heres a simple example with then values alerted via javascript so you can see whats going on -
    Code:
    <form name="badger">
    <input type="hidden" name="stoat" value="greebo">
    </form>
    
    <a href="#" onClick="document.badger.stoat.value='HUNGRY';alert(document.badger.stoat.value); return false;">EEEEK</a>
    
    
    
    
    
    <script language="JavaScript">
    alert(document.badger.stoat.value);
    </script>
    badger and stoat are just the names I used for the form and input, in case your wondering
    If I wanted to use a string variable in place of stoat, how would I rewrite the line: document.badger.stoat.value? In other words, if the variable THIS is equal to "stoat", would it be written something like: document.badger.('THIS').value?

    Also, what is the "alert" line doing?

    Thanks so much!!!!

  4. #4
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Hiya,

    all 'stoat' is is the name of your input. So if the name of your input is written as a string value using some server-side language such as asp or php, you will have to use the same string in the JavaScript.

    If your string variable is coming from JavaScript, then it gets a little trickier.

    All the 'alert' line is doing is telling us what the value of the input is, so we can tell when its changed.

    Heres the same code, but with 'stoat' being dynamically replaced with an ASP variable called 'myVar' -

    Code:
    form name="badger">
    <input type="hidden" name="<%=myVar%>" value="greebo">
    </form>
    
    <a href="#" onClick="document.badger.<%=myVar%>.value='HUNGRY';alert(document.badger.<%=myVar%>.value); return false;">EEEEK</a>
    
    
    
    
    
    <script language="JavaScript">
    alert(document.badger.<%=myVar%>.value);
    </script>
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?



Tags for this Thread