creating hidden field in javascript

Discussion in 'JavaScript' started by shekhar_v4@yahoo.co.in, May 20, 2006.

  1. #1
    hi all.
    I wants to create an html hidden field in javascript and associate it with a form.
    Can any one tell me how to do this in javascript

    thnx,
    raj
     
    shekhar_v4@yahoo.co.in, May 20, 2006 IP
  2. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    <input type="hidden" name="hiddenelement" id="hiddenelement" />
    Code (markup):
    Then in your javascript just alter the value of that element.
     
    Slapyo, May 20, 2006 IP
  3. Young Twig

    Young Twig Peon

    Messages:
    27
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Slapyo's got the idea. But if you're looking to create these fields dynamically, try something like this:
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", ".6.h46.h6j.6.0h86");
    input.setAttribute("value", "987asd9f87s9df87");
    document.getElementById("formid").appendChild(input);
    Code (markup):
    I'm rusty with DOM, so forgive me if that's wrong. :p
     
    Young Twig, May 23, 2006 IP
  4. paresh1785

    paresh1785 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This does not work in IE 7. can you give me any solution?

    Thanks
     
    paresh1785, Sep 8, 2010 IP
  5. Sulan84

    Sulan84 Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #5
    You can handle the browser problem via jQuery.
    Check the ".html" function
    $('#myForm').html('<input type="hidden" name="asd" value="asdasd"/>');
     
    Sulan84, Sep 8, 2010 IP
  6. WWSD

    WWSD Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Is there an error in IE7? how do you know it doesnt work?
     
    WWSD, Sep 19, 2010 IP
  7. Kakers

    Kakers Active Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #7
    If I remember correctly when I was looking into this for a system I was making at work.....

    I might be wrong but after a lot of research I found out that a lot of browsers stop you from turning the hidden on and off as part of a security measure. I just got so fed up in the end trying to find an answer I just used jquery and made the field disabled unless I enabled it via a checkbox :)
     
    Kakers, Sep 20, 2010 IP
  8. GFX_Lover

    GFX_Lover Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Use this code:

    
    with(i = (d = document || window).createElement("input")) type = "hidden", value = "Some value here"; d.forms[0].appendChild(i);
    
    Code (markup):
     
    GFX_Lover, Sep 22, 2010 IP
  9. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #9
    
    <form action="whatever.htm" method="get">
    <div id="mydiv"></div>
    <script type="text/javascript">
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", ".6.h46.h6j.6.0h86");
    input.setAttribute("id","txt1");
    input.setAttribute("value", "987asd9f87s9df87");
    document.getElementById("mydiv").appendChild(input);
    
    alert(document.getElementById("txt1").value)
    </script>
    <input type="submit" />
    </form>
    
    Code (markup):
    Tested in IE7 and works fine
     
    camjohnson95, Sep 22, 2010 IP
  10. ankitostwal

    ankitostwal Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanx buddy ... your solution works..
     
    ankitostwal, Jan 13, 2013 IP
  11. ankitostwal

    ankitostwal Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thanx buddy .. your solution works ..:)
     
    ankitostwal, Jan 13, 2013 IP