• 0

[JavaScript]Disable Textbox


Question

I am coding a form using ASP.

I am trying to use some javasript to control the locking/unlocking of a textbox that is populated with data from our AD.

This is an example of the code:

		Response.Write "<tr><td><input type=checkbox name='chkext' onclick=""this.form.txtext.disabled=false"" /> Phone Extension: <input type=text name='txtext' Disabled='true' value='" & oRecordSet.Fields("telephonenumber") & "' /></td></tr>"

Now everything works. When the user checks the box the textbox becomes editable. The problem is that when they uncheck the box the field stays editable. Ideally I would like it to lock.

I am guessing I need some sort of Javascript function but this is where I run into trouble.

Heres what I am kind of thinking the function would need to be.

function lockfields()
	{
		if (this.form.txtfName.disabled=false)
		{
		this.form.txtfName.disabled=True;
		}
		if (this.form.txtfName.disabled=True)
		{
		this.form.txtfName.disabled=False;
		}
	}

I was just playing around, obviously txtfname needs to be a variable and the function needs to get the variable value when the checkbox is checked or unchecked.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

something along the lines of

var chkext = document.getElementById('chkext');
var txtext = document.getElementById('txtext');

txtext.disabled = !chkext.checked;

should work. The problem with your code is that you're using a single = to test equality when checking if the textbox is enabled, you need to use == for that.

Edited by Frosven
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.