-4

I want to show result of first four characters from text field 1 + field 2 full in javascript + HTML

Here is the sample code

<form onsubmit="return false" oninput="txtFullName.value = txtFirstName.value +''+ txtLastName.value">
First name : <input type="text" name="txtFirstName" /> <br><br>
Last name : <input type="text" name="txtLastName" /> <br><br>
Full name : <input type="text" name="txtFullName"  > <br><br>
</form>

but in full name i want only to display first four characters of first name + last name full

if possible in full last name space i need to show dob selection

CJ Junior
  • 31
  • 3

3 Answers3

1

You can use substr(0, 4) to get the first four characters for each fields.

<form onsubmit="return false" oninput="txtFullName.value = txtFirstName.value.substr(0, 4) +' '+ txtLastName.value.substr(0, 4)">
First name : <input type="text" name="txtFirstName" /> <br><br>
Last name : <input type="text" name="txtLastName" /> <br><br>
Full name : <input type="text" name="txtFullName"  > <br><br>
</form>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • thanks ankit, could u help me more?? In wordpress when i use this code onclick event disappears when I switch the visual and text tab. Any fix on this, please me with creating external ones. – CJ Junior Mar 26 '18 at 11:37
  • @CJJunior you can tick mark the answer if it was helpful to you – Ankit Agarwal Mar 26 '18 at 11:52
1

You only need to make a simple change... using substr() will do the trick.

<form onsubmit="return false" oninput="txtFullName.value = txtFirstName.value.substr(0, 4) +' '+ txtLastName.value">
First name : <input type="text" name="txtFirstName" /> <br><br>
Last name : <input type="text" name="txtLastName" /> <br><br>
Full name : <input type="text" name="txtFullName"  > <br><br>
</form>
Neil Docherty
  • 555
  • 4
  • 20
  • thanks , could u help me more?? In wordpress when i use this code onclick event disappears when I switch the visual and text tab. Any fix on this, please me with creating external ones. – CJ Junior Mar 26 '18 at 11:43
1

Try this:

var fName = document.getElementById("txtFirstName").value.substr(0,4);
var lName = document.getElementById("txtLastName").value;
document.getElementById("txtFullName").value = fName + " " + lName;
AdrianL
  • 9
  • 3