Javascript:disabled textfield when other field match certain word

I"m new in using javascript and trying to make registration form using java script/j query that when some user typing some word then the next input field will be automatically disabled. Example: User typing "Bread" in the field then next field go blank and can't be use. I have try searching it on search engine but the answer only about disabling submit button.Below is my code:

    <script>
    function matchWord(e){
    console.log(document.getElementById('food').value.trim());
    if(document.getElementById('Food').value.trim() == "Bread"){
    document.getElementsById('OFood')disabled==true;
    document.getElementsById('PFood')disabled==true;
        }
    }

    document.getElementById('food').onkeyup = matchWord;
    </script>

and this is my HTML:

Please Insert other food then bread

    Food:<input type = "text" name="food" id="food" value ="" size ="22" placeholder ="Enter your Favourite food"/><br>
    Drink:<input type = "text" name="Ofood" id="Ofood" value="" size="22" placeholder="Enter your Favourite Drink"/><br>`
    Restaurant:<input type = "text" name="Pfood" id="Pfood" value="" size="22" placeholder="name your best restaurant"/>

I also try to run the code,but it don't work. I need a solution. That all thank you.

Answers 1

  • Lots of Typos in your script

        function matchWord(e){
        console.log(document.getElementById('food').value.trim());
        if(document.getElementById('food').value.trim() == "Bread"){
        document.getElementById('Ofood').disabled=true;
        document.getElementById('Pfood').disabled=true;
            }
        }
    
        document.getElementById('food').onkeyup = matchWord;
     Food:<input type = "text" name="food" id="food" value ="" size ="22" placeholder ="Enter your Favourite food"/><br>     Drink:<input type = "text" name="Ofood" id="Ofood" value="" size="22" placeholder="Enter your Favourite Drink"/><br>`     Restaurant:<input type = "text" name="Pfood" id="Pfood" value="" size="22" placeholder="name your best restaurant"/>


Related Articles