javascript to disable button

Discussion in 'JavaScript' started by RDJLabs, Jan 23, 2010.

  1. #1
    I want to disable submit button using javascript if textbox is blank and enable it when someone fills textbox. Is it possible to do it in javascript?
     
    RDJLabs, Jan 23, 2010 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    following example would be helpful..

    
    <html>
    <head> 
    <script language="javascript">
    function disab()
    {
    	frm=document.forms[0];
    	if(frm.tbox.value.length <= 0) 
    	{
    		frm.button1.disabled=true;
    	}
    	else
    	{
    		frm.button1.disabled=false;
    	}
    }
    </script>
    </head>
    <body text="000000" bgcolor="ffffff">
    <form>
    <input name="tbox" type="text" id="tbox" value="" onkeyup=disab();>
    <input type="button" onclick="alert('button pressed!')" value="button" name="button1" disabled="false">
    </form>
    </body>
    </html>
    
    HTML:
     
    mastermunj, Jan 23, 2010 IP
  3. alexpr07

    alexpr07 Active Member

    Messages:
    284
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Nice example above but I would replace "onkeyup" with "onchange"
     
    alexpr07, Jan 27, 2010 IP
  4. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does it matter?

    I ask because someone suggested I use that in a character-counting script where people filled in a text area and could see how many chars they've typed... they suggested it because they said, "what if people copy and paste instead of type?" but I tested it and it didn't matter... ctrl+v was also a keyup event.

    But I would like to hear more reasons why, as I do keep hearing that suggestion.

    I thought onchange was more for selects?
     
    Stomme poes, Jan 28, 2010 IP
  5. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Same, I mainly use onChange for selects only. Would love to hear an explanation :)
     
    Dondon2d, Jan 28, 2010 IP