spouki Posted June 8, 2009 Share Posted June 8, 2009 I have a checkbox and a text field. My goal is to hide the value of the text field when the checkbox is checked. e.g. <form name="form1" action="somepage.php"> <input type="text" name="text1"> <input type="checkbox" name="checkbox1" onclick=" document.forms[0].text1.value= ' I want to hide this text ' ; some_function_to_hide_text(); "> </form> So is there some sort of function in Javascript that can do this? Your help would be appreciated.. Quote Link to comment Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 Something like; <form name="form1" action="somepage.php"> <input type="text" name="text1" id="textInput" /> <input type="checkbox" name="checkbox1" onclick="hideTextInput(this);" /> </form> <script type="text/javascript"> function hideTextInput(obj) { textInput = document.getElementById('textInput'); if(obj.checked) { textInput.style.display = 'none'; } else { textInput.style.display = 'block'; } } </script> Not tested Quote Link to comment Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 Or, to make ait look a little nicer, just disable the text box; <form name="form1" action="somepage.php"> <input type="text" name="text1" id="textInput" /> <input type="checkbox" name="checkbox1" onclick="hideTextInput(this);" /> </form> <script type="text/javascript"> function hideTextInput(obj) { textInput = document.getElementById('textInput'); if(obj.checked) { textInput.disabled=true; } else { textInput.disabled=false; } } </script> Quote Link to comment Share on other sites More sharing options...
spouki Posted June 8, 2009 Author Share Posted June 8, 2009 your code is working gevans, but it seems you didn't understand my question, my goal is not to disable the text field, but instead i want to hide the inner text Quote Link to comment Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 So you want to empty the text field when the checkbox is checked, then put it back in place when its unchecked? Quote Link to comment Share on other sites More sharing options...
spouki Posted June 8, 2009 Author Share Posted June 8, 2009 Actually I want the text field to have a value that is not visible unless it is shaded or something like that Quote Link to comment Share on other sites More sharing options...
Maq Posted June 8, 2009 Share Posted June 8, 2009 Change the type to password. Quote Link to comment Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 Ok, you're going to have to explain this to me a bit better. - page loads - input field is there but has no value? - checkbox is clicked and the value is loaded into the input field? - check is again and it disapears ?? Quote Link to comment Share on other sites More sharing options...
spouki Posted June 8, 2009 Author Share Posted June 8, 2009 When the checkbox is checked, the input field is disabled and a certain value is loaded into the input field (but it is not visible to others) <input type="text" name="textbox"> <input type="checkbox" name="checkbox1" onclick="this.form.textbox.disabled= this.checked; this.form.textbox.value='99'; " > // making "99" not visible is all what i need Quote Link to comment Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 I'm sorry, I could do little code tweaks all day, but I'm not fully udnerstanding your requirements, I'll leave it to someone that may be finidng this clearer than myself. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.