SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "hidden" variables declared in JavaScript

    I was wondering if it is possible to declare a hidden form variable in JavaScript instead of declaring it <from><input type=hidden ...></form>? Thanks
    -: Arnold Nicolas :-
    www.filgoods.com

  2. #2
    SEOChat has too many ads Protoss's Avatar
    Join Date
    Aug 2004
    Location
    Long Island, NY
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can declare a variable outside of a function, then reference it from other functions and actions on the page. However, there's no way to track it after you've submitted a form (if that's important to you in whatever your scenario is).

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually do need to grab that information after it has been submitted. The whole scenario for me is that I have several form pages that I would need to update if I were to declare a hidden variable within the <form></form> tags. All of my form pages use 1 common JavaScript file. So I was just wondering if it is just possible to just declare it within the JavaScript and track that information as soon as it is submitted.
    -: Arnold Nicolas :-
    www.filgoods.com

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are talking about dynamically creating a <input type="hidden"> from javascript, check out this example:
    test.html
    Code:
    <html>
    <head>
    <script language="JavaScript">
    function AddHidden(theform, thename, thevalue)
    {
    	var oHidden = document.createElement("input");
    	oHidden.setAttribute("type", "hidden");
    	oHidden.setAttribute("name", thename);
    	oHidden.setAttribute("value", thevalue);
    	theform.appendChild(oHidden);
    }
    </script>
    </head>
    <body>
    <form id="theform" method="get" action="test.html">
    <input type="submit" name="thebutton" value="Click Me" onclick="AddHidden(document.getElementById('theform'), 'testname', 'testvalue');">
    </form>
    </body>
    </html>

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually do need to grab that information after it has been submitted.
    Your other option is to use js to tack the name/value pair onto the url of the pages listed for your form's action attribute, and have your php page or whaterver server side language you are using extract the name/value from the url.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jimfraser,

    YOU ROCK!!!

    It's exactly what I need. Awesome forum site.
    -: Arnold Nicolas :-
    www.filgoods.com


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
  •