+ 2

How do I manipulate user input as a string? ANSWERED

Edit: I'm a total idiot. I originally wrote the program in Java and translated it to JS but I didn't copy and paste two lines of code so the whole thing screwed up. smh Question: What type of variable is user input (string, integer, etc.)? because charAt and substring does not work. I'm trying to figure out how to get user input from HTML as a string variable. I can access what the user inputs into the text box and print it to the page, but I want to manipulate it as a string. Like if I try to find a certain character with charAt, it won't let me. Here's the form HTML and script. This is not the entire code, so it will not work if you copy and paste. It is meant to give you a clue of what I'm trying to do. <form id = "inputForm"> Enter something in the box: <input type="text" name="textInput"> </form> <button name="button" onClick="setUp()">Convert</button> function setUp() { var formTemp, w; formTemp = document.getElementById("inputForm"); w = formTemp.elements["textInput"].value; document.write(w.charAt(0)); //I get a "Uncaught TypeError: Cannot read property 'charAt' of undefined" }

21st Dec 2017, 5:58 PM
Henry Waddill
Henry Waddill - avatar
3 Answers
+ 9
it would make it a lot easier for us to help you if you could post the whole code (a link to the code playground) im not sure about the .elements method (?) you should avoid accessing elements like that, you might want a dynamic Web page where dem DOM changes you could 1) use .chilNodes instead 2) access the input field directly (with an Id for example) note: 1) if you use a form the button should be inside it as well and the type should be submit (semantically correct) 2) don't use onclick, the javascript addEventListener method is way better (more control + less code)
19th Dec 2017, 10:22 PM
Kamil
Kamil - avatar
+ 3
Hello ! you need "Value" ↓↓↓ :) <input type="text" id="text"/> function Func() { var x = document.getElementById("text"); //here "value" ↓↓↓ :) if(x.value == "123456") { alert("Good"); } else { alert("incorrect"); } }
19th Dec 2017, 5:12 PM
James16
James16 - avatar
0
the value is always empty for me...
19th Dec 2017, 5:58 PM
FA_develops
FA_develops - avatar