Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to set display attribute of <input>

Status
Not open for further replies.

rocker24

Programmer
Oct 10, 2000
3
I am working from an .html page. I want to check an input and if nothing is entered into it (i.e. value.length = 0) then I want to hide it. It will not let me set the display to none.

I have checked the value before I try to set it and it will successfully retrieve it. I just cannot change it.

Any suggestions?
 
It will not let me set the display to none.

What is "it"? JavaScript and browsers have no such restriction - you should be able to set the display of the input to "none" with no problems at all.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Assuming markup like this:
Code:
...
<input type="text" id="typeIn" value="">
...
You can do this:
Code:
...
var myTypeIn = document.getElementById('typeIn');
if (myTypeIn.value == '') {
  myTypeIn.style.display = 'none';
}
...
Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Another scheme that I use quite a bit is called CSS switching.

In your CSS (either within <style></style> or in a separate file), put

.hid {display: none}

Then when you want to make the element invisible, do this

MyTypeIn.className = "hid"

To make it reappear,

MyTypeIn.className = ""

More complex schemes also work, because you can assign more than one class to an element; just separate the class names with spaces.
 
Thanks for all your help.

I knew it should work but couldn't figure out why it wasn't working. The having a break and looking at it next day solved the problem. I had forgotten the quotes around the none. I can't believe it was something so stupid!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top