views:

60

answers:

3

Is it possible to change the value of an <input type="text"> that has been hidden with a style of display:none? I have some JS that seems to work when the input is <input type="hidden"> but not when it's hidden with display:none. And AFAIK, you can't change an input's type with JS either.

Basically, I want to replace an <input> with a <select>, so I'm trying to hide it and append the <select> element.


Take a look at http://jsfiddle.net/5ZHbn/

Inspect the <select> element with firebug. Look at the hidden input beside it. Change the select's value. The hidden input doesn't change. Is Firebug lying to me?

If you uncomment the other lines of code, then it works.

Actually... I'm pretty sure it is a bug in Firebug now. Most other things correctly update, but firebug doesn't show the updated value when I inspect it.

+3  A: 

Changing a field's value should work as expected, regardless of any CSS styling. The issue is likely elsewhere.

Matchu
agreed. JavaScript has full control to set the value at any time.
scunliffe
@scunliffe: That's not entirely true. I know for sure that you can't change the value of `file` inputs.
Mark
@Mark — I think @scunliffe was talking in the context of fields that Javascript can change at all.
Matchu
@Matchu: Well that's exactly what this question is about... whether or not it can be changed at all ;) Anyway, I think firebug was deceiving me. Thanks for the answer.
Mark
@Matchu - agreed the file input type is special and can not be tinkered with for security reasons... but all other input's *should* have no issue. I say *should* as there are ways of shooting yourself in the foot in IE ;-)
scunliffe
+2  A: 

You can change it as usual:

document.getElementById( 'myinput' ).value = 'Hello';
dionyziz
A: 

One option you have is putting the input box inside a div and then using javascript to change the contents of the div. For example:

<html>
<head>
<title>Input Text To Dropdown Box</title>
<script type="text/javascript">
function swap() {
document.getElementById("contentswap").innerHTML = "<select><option value='cats'>Cats</option><option value='dogs'>Dogs</option></select>";
}
</script>
<style>
#contentswap {
display:inline;
}
</style>
</head>
<body
<div id="contentswap">
<input type="text" name="original">
</div>
<br />
<input type="button" value="Input To Select" onClick="swap()">
</body>
</html>
Building429