简体   繁体   中英

Split text box text value in before “@” in Javascript

Here i have txtemailid and txtlogin(read only) what i type in txtemailid before @ text it will directly go to txtlogin text box when i will happen means when i type something in email text box and next click to mouse without entering the user. how can i do this java and i take code in java script it's not working

Thank you

It is better you use keyboard event to make changes on each time a key is pressed.I am doing using onkeyup .I prefer document.getElementById() rather than document.form because it can accurately retrieve it, every time, without caring about what happens to the document in the meantime.You can use either of the two.

JSFIDDLE

Here is the code:

 function myFunction() { var text1 = document.getElementById("txtEmailId").value; var result = text1.split("@"); document.getElementById("txtLoginname").value = result[0]; } 
 <form> Enter Email: <input type="text" id="txtEmailId" onkeyup="myFunction()" /><br/><br/> Login Name: <input type="text" id="txtLoginname" /><br/><br/> </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM