SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem in enabling a textbox

    hi there i have this code using a radio button to enable and disable text but for some reason the disable button doesn't work,can anyone help me?when i chose the 1st radio button the other textbox must be disabled and so on..

    Code:
    <html>
    <head>
    <title>Disable Text Box</title>
    <script language="JavaScript">
    function DisBox(fldobj,txtbox)
    {
    txtbox.disabled=eval(fldobj.value);
    (txtbox.disabled==false) ? txtbox.focus(): fldobj.focus()
    
    
    }
    </script>
    </head>
    <body bgcolor="lightgreen">
    <center>
    <form name="Distbox">
    <input type="radio" name="disb" value="false" onClick="DisBox(this,document.Distbox.txtb)">
    <input type="text" name="txtb" size="30" disabled="true">
    <input type="radio" name="disb" value="false" onClick="DisBox(this,document.Distbox.txta)">
    <input type="text" name="txta" size="30" disabled="true">
    </form>
    </center>
    </body>
    </html>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it looks like you want to iterate through all the radio buttons and disable/enable the appropriate field based on each "checked" value. Try this:
    Code:
    <html>
    <head>
    <title>Disable Text Box</title>
    <script language="JavaScript">
    function DisBox(fldobj)
    {
    	var radios = fldobj.form.elements[fldobj.name];
    	for (var i=0; i < radios.length; i++) {
    		var txtbox = radios[i].nextSibling;
    		while (txtbox.nodeType == 3) { // make sure we get an element not a text node
    			txtbox = txtbox.nextSibling;
    		}
    		txtbox.disabled = radios[i].checked ? "" : "true";
    	}
    }
    </script>
    </head>
    <body bgcolor="lightgreen">
    <center>
    <form name="Distbox">
    <input type="radio" name="disb" value="false" onclick="DisBox(this)">
    <input type="text" name="txtb" size="30" disabled="true">
    <input type="radio" name="disb" value="false" onclick="DisBox(this)">
    <input type="text" name="txta" size="30" disabled="true">
    </form>
    </center>
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •