Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  197] [ 1]  / answers: 1 / hits: 15393  / 14 Years ago, tue, november 23, 2010, 12:00:00

I need to hide a text input field with javascript. Changing its type attribute to hidden does not work in IE (security issue).



What would be the best way to do it?



Note: No jQuery or other lib can be assumed.


More From » html

 Answers
163

I assume you have to show and hide the text field dynamically based on changing conditions in the form, otherwise you'd just make it an <input type=hidden... to begin with.



Keep your code that shows and hides the field as it is, but also catch the onsubmit event.



In the submit handler, get your text field via document.getElementById(...) (or by accessing document.forms[i]) and check to see whether or not it's hidden.



If it is hidden, create a new DOM node for an <input type=hidden ...> field and add that node to the form, probably via myform.appendChild(...). You'll have to give it the name your server-side code expects. Copy the contents of the hidden text field into the newly created type=hidden field, then return from your submit handler, allowing the standard submit to continue.



You could also just un-hide the text field on submit, but you'd have to move it off screen also or the user would see it reappear during submit processing.


[#94867] Saturday, November 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billt

Total Points: 608
Total Questions: 100
Total Answers: 87

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
billt questions
Fri, Sep 10, 21, 00:00, 3 Years ago
Tue, Aug 18, 20, 00:00, 4 Years ago
Sat, Apr 6, 19, 00:00, 5 Years ago
;