Forum Moderators: not2easy

Message Too Old, No Replies

How to enable/disable HTML Form Fields using CSS

How to enable/disable HTML Form Fields using CSS

         

krishire

10:53 am on Nov 26, 2003 (gmt 0)

10+ Year Member



1.How to enable/disable HTML Form Fields using CSS.
2.How to make HTML Form Fields visible/invisible using css
2.1 Is the visibility property for the HTML Form fields available in CSS

Please help me out.

BlobFisk

11:43 am on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, krishire!

  1. You would need to use a scripting language, like Javascript, to manipulate the visibility or display attribute of the input element.
  2. Again, you would need to use a scripting language to do this. Something like document.getElementById('inputID').style.visibility='visible'; is the approach needed.
  3. Yep - you can toggle the visibility or the display of the input element using a unique ID.

HTH

krishire

11:59 am on Nov 26, 2003 (gmt 0)

10+ Year Member



In the Server Side, I want to set a Css Class for the HTML Server side controls(DOTNET World!), so that It gets disbled/enabled/visible/invisible when it reaches the client browser. In this scnerio I don't to call a page load javascript to iterate and then do the stuff..

krishire

12:01 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Also,
I wrote,

.Vis1
{
visibility:visible;
}

.Vis0
{
visibility:hidden;
}

Which works fine for making visible/invisible.

But for disabling/enabling?

BlobFisk

12:02 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, maybe applying a class server side to make the input invisible when needed:


.hideMe {
visibility: hidden;
}

This will make the element invisible - you could also use display:none. Visibility will still create a 'space' for the input element, whereas display will remove it completely.

HTH

BlobFisk

12:03 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just saw your other post, how do you mean enabling/disabling? Making the input editable/uneditable? To make it uneditable use disabled="disabled".

krishire

12:17 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Can I add this "disabled:disabled" to my CSS Class.

BlobFisk

1:58 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No - disabled is an html attribute not a CSS one.

krishire

2:05 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Thanks blob,
I want a CSS Class for a html form field doing the editable/uneditable work, when the class is set for the
form field. CAn I call a javascript function from a css class.

BlobFisk

2:08 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, no I'm afraid! You can control the position and appearance of an input element, but at the moment not it's function (including whether it is disabled or not).

Nor can you call a JS function from your CSS....

chadmg

3:23 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Since I don't really know what you're doing this might not help you, but you said you wanted to do this server side using css so I'll throw out a suggestion.

You could use css to make a div look like an input field and that wouldn't be editable. So you can have an input field and your fake input div. Make the input display:inline and the div display:none when you want them to enter text. Then after they submit your form and the page is reloaded, make the input display:none and the div display:block or display:inline and you can use server-side code to read the post from your form and display it inside the div. So it will look like an input field but won't be editable, all using server-side code and css. You can even play with the css for your div to give it a grey background and different colored text.

Don't know if this helps, but it's an idea.

<added>Of course, you could just use server-side code to add "disabled" to your input field. I don't know why you would need to use css.</added>

DrDoc

3:27 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well... you can always enable/disable/re-enable your input fields using JavaScript.

document.getElementById(id).disabled = true;
document.getElementById(id).disabled = false;

krishire

3:39 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Just to Explain you want I'm doing.

In dotnet(ASP.NET), we can set the properties of the server side control at runtime(That is at the server side).

Depending upon the business logic, I need to make controls in my page Editable/Non-Editable - Visible/invisible - Mandatory(Changing background color of a control)

For doing this,

For Example, for a textbox server side control(which emits html textbox field to the clientside)

I need to write , three lines of code

textbox1.visible = true/false
textbox1.enabled = true/false
textbox1.cssclass = "Classname" - to made mandatory color change

Instead,
I want to write css classes with the above three combination
and just set the cssclass - one line instead of three lines.

Hope you now understood my requirement, thanks a lot for spending your time for. me

DrDoc

3:41 pm on Nov 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah! Makes sense now.

No, you cannot change whether an input field is editable or not using CSS. The HTML attribute is what controls that, and it has nothing to do with CSS. It can be set/changed using JavaScript too, but not using CSS.

chadmg

3:58 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



You can always wait for css3 and hope :)

krishire

4:01 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Since css is the part of DHTML, why not mine.

chadmg

5:03 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



dhtml requires client-side javascript

garann

6:08 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



krishire, I don't know if this helps, but when I need to determine server-side whether the fields in a form will be editable, I use a variable that gets written into the input elements. Looks like:


<script runat="server">
Dim isDisabled as String = ""
Sub Page_Load(...)
if [some circumstances..] then
isDisabled = " disabled"
end if
...
</script>
...
<input type="text" name="myVal1"<%= isDisabled %>>
<input type="text" name="myVal2"<%= isDisabled %>>

It's messy, but it works.

A more elegant solution might be to subclass the .NET Server Controls you're using and override their default value for disabled, depending on your business logic. So you'd create something like <krishire:TextBox id="myVal1"> from a class that inherits all the values of the TextBox control, but overrides the Enabled property if necessary (you should be able to do this by making that property static).

hth,
g.

krishire

6:56 pm on Nov 26, 2003 (gmt 0)

10+ Year Member



Dotnet has got enable attribute/property for every server side html controls through which we can enable or disable.
But my requirement was to call a cssclass function which does 3 line work(mentioned above) in just one line of assigning a cssclass to the control. Anyhow thanks, I will use two lines now, one to enable/disable and another one to cssClass which takes care of visiblity and setting mandatory color change

Textbox1.cssclass = "Mandatory&visibility"
Textbox1.enabled = true