Call function when button on click | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Call function when button on click

Hi everyone, Now I writing some JS codes and I've some issues. I create a text box and button in html as follow: <input id="txtuname" type="text" name="uname" class="txtbox" /> <input name="btnLogin" type="button" class="btn" value="Login" onclick="chkblank" /> I would like to alert messages by clicking button when the text box is empty text. So, I wrote follow scripts but it not working. <script type="text/javascript"> function chkblank(){ var bc=document.getElementsByName("uname"); if (bc.value==""){ alert("Username is blank"); }; } </script> Could you please help with some ideas for me? Thanks. Ko Dway

29th Mar 2019, 3:40 PM
Ko Dway
Ko Dway - avatar
2 Answers
+ 4
://You have missed many things in html anddl css like taking value from tag name but from invalid etc //solved <input type="text" id="uname" class="txtbox" /> <input type="button" class="btn" value="Login" onclick="chkblank()" /> <script> function chkblank(){ var bc=document.getElementById("uname").value; if (bc==""){ alert("Username is blank"); }; } </script>
29th Mar 2019, 4:00 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 2
Great!! I thought the value attribute need to add in if statement. Thanks a lots and already solved. 😁 Ko Dway
29th Mar 2019, 4:15 PM
Ko Dway
Ko Dway - avatar