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
<input type="hidden" name="hiddenelement" id="hiddenelement" /> Code (markup): Then in your javascript just alter the value of that element.
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.
You can handle the browser problem via jQuery. Check the ".html" function $('#myForm').html('<input type="hidden" name="asd" value="asdasd"/>');
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
Use this code: with(i = (d = document || window).createElement("input")) type = "hidden", value = "Some value here"; d.forms[0].appendChild(i); Code (markup):
<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