Home > Net >  disabled button not working properly in react
disabled button not working properly in react

Time:12-17

if give more than 10 characters then button should be disabled but when we give <10 character's button should be enabled not working

import React, { useState } from "react";
export default function DropDown() {
  let [disable,setDisable]=useState(false)
  function check(){
    let inp=document.querySelector('input');
    let btn=document.querySelector('button');
    if(inp.value.length>10){
      btn.disable=true
      setDisable(true)
    }else{
      btn.disable=false;
      setDisable(false)
    }
  }
   return (
    <>
    <input disabled={setDisable} onKeyUp={check}/>
<button>click</button>
    </>
  );
}

CodePudding user response:

just change <input disabled={setDisable} onKeyUp={check}/> to <input disabled={disable} onKeyUp={check}/>

import React, { useState } from 'react';
export default function App() {
    const [disable, setDisable] = useState(false);
    const check = ({target}) => {
    if (target.value.length > 10) {
      setDisable(true);
    }
    else {
      setDisable(false);
    }
    }
    return (
        <>
            <input disabled={disable} onKeyUp={(e) => check(e)} />
            <button>click</button>
        </>
    );
}

if you want to disable button just add

<button disabled={disable}>click</button>
  • Related