siddartha N
siddartha N

Reputation: 1

not getting alert even when there's no text in input filed

At first, if I click the button with no text in the input field it prompts error message. but if I send a message with some text and then try to send an empty message it doesn't promptly alert.

  const ip = document.getElementById('ip');
        const btn = document.getElementById('btn');
        
        const sm = document.getElementById('sm')
          btn.addEventListener('click' ,send);
        
          function send(e){
              if(ip.value===""){
                  alert("can't send empty message");
               
              }
              else{
              var k = ip.value;
              
              sm.innerHTML = k;
              ip.value = " ";
              
          }
          
        }
            <div class="container">
           <h1 id='msg'> Send Message for 
            free </h1>
            <input type="text" id='ip'>
            <button id="btn" type='button'>send</button>
            <h2 >sent message</h2>
            <h2 id='sm'> </h2>
        </div>

Upvotes: 0

Views: 62

Answers (1)

harish kumar
harish kumar

Reputation: 1759

Try below change:

 if(ip.value.trim()===""){
      alert("can't send empty message");              
  }

Upvotes: 1

Related Questions