Grabbing the first character of the input field value. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Grabbing the first character of the input field value.

In the below code how can I grab the first character of the input field and do something with it(I want to create a list item with that character in the Dom) I had log the the whole value of the input field but I want only the first character. https://code.sololearn.com/WVPOwF9RB5Bm/?ref=app

3rd Aug 2019, 11:21 AM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
2 Answers
+ 5
You can get first character with adding [0] after variable name. You can change line 4 like below code to get first charachter: returnVal = input.value[0];
3rd Aug 2019, 11:44 AM
ΛM!N
ΛM!N - avatar
0
Use charAt() method which accepts a number. It's a position of a character. Positioning starts from zero.
4th Aug 2019, 3:53 PM
Timur Myngbay
Timur Myngbay - avatar