Andre Hofmeister
Andre Hofmeister

Reputation: 3416

Disable input submit button with js in IE & FF doesnt' work

I have a problem, with disabling a input button with javascript in a aspx document at ie.

The js look's like

<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) 
{
    if (searchinput.value.length < 4) 
    {
        document.getElementById(target).disabled = true;
    }
    else 
    {
        document.getElementById(target).disabled = false;
    }
}
</script>

I call the input button with

<input name="searchinput" type="text" value="" id="searchinput" onkeyup="SetButtonStatus(this, 'searchsubmit')" />

In Chrome everything works fine. If i type more then 4 characters in the inputfield, the button will be enabled. But in IE & FF nothing happens... Why? How could i fix this?

Upvotes: 0

Views: 725

Answers (3)

Quentin
Quentin

Reputation: 943996

You are depending on the non-standard "Create a global variable for every element that has an id" that is supported by Chrome and IE in some rendering modes.

Replace searchinput with sender (since you have defined sender and passed a reference to the element you are interested in already).

Upvotes: 2

Alexander Beninski
Alexander Beninski

Reputation: 817

Why don't you just use jquery it handles all browsers internaly and you don't have to worry about them. Make it like this:

<input name="searchinput" type="text" value="" id="searchinput"/>
<input type="button" value="button" disabled="true" id="buttonSearch"/>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>  

<script type="text/javascript">

    $(function(){
      $('#searchinput').keydown(function(e){

        var lenght = 3;

        if(e.keyCode ==8)
        {
            lenght = 5;
        }

         if (searchinput.value.length < lenght) 
          {
            $('#buttonSearch').attr("disabled", true);
          }
          else
          {
           $('#buttonSearch').removeAttr('disabled');
          }

       });

    });
</script>

Upvotes: -1

Andrew D.
Andrew D.

Reputation: 8220

<script language="javascript" type="text/javascript">
function SetButtonStatus(sender, target) 
{
    // use document.getElementById("searchinput") instead of searchinput or in your case can use sender 
    document.getElementById(target).disabled = document.getElementById("searchinput").value.length < 4;
}
</script>

Upvotes: 2

Related Questions