Target a Class within an Input field text box?


(Stephen G) #1

Hi I have a text box with an input field using the following HTML:

<input class="user-input" type="text" name="userAttempt" value="" >

I want to check the value entered by running the following when a submit button has been pressed:

var userAttempt = document.getElementsByClassName("user-input").value;
userAttempt = userAttempt.toLowerCase();

console.log("User Input was: " + userAttempt);

However this is returning an undefined variable.

Can anybody tell me where I am going wrong?

I have NO CLASS APPLIED within the property inspector, just within the input field HTML inside the text box.

Many thanks,
Stephen.


(Mark Hunte) #2

getElementsByClassName returns an Array

var userAttempt = document.getElementsByClassName("user-input")[0].value
userAttempt = userAttempt.toLowerCase();
console.log("User Input was: " + userAttempt)

(Stephen G) #3

Thanks a million @MarkHunte usually an obvious answer! Next time ill look over the documentation first before posting…but thanks for the reply :beers: