Learn JavaScript #15 | Textareas and Getting Values of Inputs

JosephDelgadillo
58 followers
23 Video Views·Sep 3, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
Now that we are all done covering arrays, let's continue learning about how to use JavaScript to interact with a web page. In this tutorial video we will cover textareas and how to get the values of inputs in JavaScript.
Learn more about my courses at https://josephdelgadillo.com
Alright! So, that is arrays, and this is a pretty good use case if you want to
basically just combine or group certain information together in one variable, in
a list, that's when you use arrays. Now let's say we want to have kind of an
object that that can have different values on different keys, so we can say
the objects name is Nick, the age is whatever, and then the hobbies like that.
And how we're going to do that is...let me actually drag this over to that tab
right there, there we go. How we're going to do that is we're basically just going
to, I think I forgot to do something actually, oh, I was just going to finish
up...okay. Before we get into objects, I apologize about that, we're gonna get
into interacting more with the web page first. So I'm going to jump up here, and I
meant to do this a few video videos ago and then I got sidetracked by the arrays,
so at least we know how to do arrays now. I'm gonna create an input with the class
of "my-input" I'm going to close it and I'm going to skip down a line using the
BR element. Save it, and refresh. There we go. So, what I want to happen here is I'm
going to type in something into the input, and then I'm going to get the
button, and when we click the button what we're going to do is we're going to
change this text to basically whatever I type into the input. So, in this case,
we're learning how to interact by grabbing a value and then setting the
inner HTML of an item. So, when this button clicks we don't want to do any of
this...well, let's let's leave the button clicked. What we're going to do is
we're going to first get this element by the class, so it's my input, so document.
getElementByClassname, and then the class name is "my-input"
End that line right there. So this is going to get that element. Now what we
can do is we can then get that value. So we need to store this in a variable, so
let's create a variable called customText, and then we're going to get the
value of that by...actually, let's do that right here. Or, no, okay. Just wait. What
we're going to do is we're going to set this HTML for the ID of text. So,
var results = document.getElementById and it's going to be "text"
And then what we're going to do is we're going to say results.innerHTML, and
this is a variable that we're setting, so go equals and then we're going to
basically access whatever was typed into the input which it's stored in the
variable custom text. So this is the input field there, so what we need to do
is type current, or sorry custom, Text.value. and then semicolon. So, let's save
this, and I'm going to refresh the application here, and I'm going to type Hello
world, then hit go. It says undefined. So let's open up the console there, I'm
pretty sure I missed something...customText is not defined. Alright, let's console.log
customText and find out exactly what I am...oh, you know what it is? This is going
to give us back a list of all elements with the class. So as you can see it's a
list here, and this one right here is at index 0. So, we're getting all the
elements and it's returning as an array. So what we need to do is we need to
target the first index in that array. So I'm going to refresh here, and I'm going
to type something here just gibberish, and when we hit go it's
going to grab that value, and then it's going to set it
right here. Now, we can go a step further. We can concatenate this to make it a
little more involved. So what I'm going to do is I'm going to set the
placeholder, so that people know what to type in, and it's going to be your name. And then what
we're going to do here is we're going to set the HTML of results to "Hello, " and
then their name. So I'm going to save this, and then I'm going to refresh. So
I'm going to say my name is Nick, and when we hit go it says Hello, Nick. So, that's
how to grab a value from an input, and it also works on select elements, and
checkboxes, and as far as textareas which we actually haven't talked about.
So let's go ahead and talk about that right quick as well. I'm going to drop
down here and create a textarea. This is basically like a large input field for
text except it's multi-line and it follows some different rules. So as a
textarea it's not like the input area in the sense that it self closes. So I'm
going to add a placeholder here and type "Your message" and it closes like
this. So if I save this and refresh it we see the textarea there. Now what happens
if you put text between the textareas, between the opening and closing tag, is
that content is prefilled, so you guys know. How you do that with an input field
is you just set the value property. So, in this case, I'm going to set the value to
"Nick" save it and refresh, and we get Nick prefilled into that input. And these are
things that we're going to get more into after as well. I'm
trying to show you guys basic usages before we start building projects, and
then once we start doing that we're going to really expand on all the basic
stuff that we learn here right now. So I'm going to give the textarea a class
of my-textarea, and in the style sheet I'm going to style it the same as my
input. So, .my-textarea. So, again, what this means basically is both of these
elements are going to have these styles. I'm sure if I explained that before? Oh, yes, I
did. Okay, cool. I must have explained that here, I think. Alright, so, let's go ahead
and refresh, and there now we've got your name and your message, and this is resizable.
There's a CSS property for this called resize, and we can choose either a
direction or none. So I'm going to set resize to none. Save, refresh, now you see
that this is not resizable. Alright, so, what we need to do is we need to jump
back to our JavaScript here, and instead of saying hello what we're going to do
is...actually, that's okay. Alright, so, let's go ahead and let's
get the results of the textarea, so var textArea. It's going to be equal to
document.getElementsByClassName, "my-textarea" And then what I'm going to
do down here I'm going to start spacing these properly. I'm going to set a line
break on to this, actually. So, just a
regular line break. You can write HTML code within text, or within a string of
JavaScript, by the way. It's definitely possible. So we're going to skip a line
and then we're going to say results.innerHTML is going to be +=
"Message: " and then textArea, which is our variable, it's going to be a list that
returns with all elements with a class my-textarea. So we're going to target
number zero because we know that's what we need. One sec...alright. And then the
value in here is just text. So I'm going to save it, we're going to refresh, I'm
going to say Nick and then my message is I like programming, and then I'm going to
hit go. And we've got undefined again. So, let's go ahead and console.log out
the textArea, save it and refresh, and I'm going to type Nick, whoops, there we go. So,
we do have this, it's stored there. We could get the HTML of it...it's getting the
placeholder, it's getting the text length, text content, tag name, where is this? Oh,
it is the value. Okay, sorry, did I say text? Okay, no. Okay, so, text is for
elements like for a div or span. Inputs and textareas always use value because
it's a value that you're setting.
There we go. So as you can see we can get the value of textareas, inputs ,checkboxes,
whatever you want to grab. That's how you can get that. Now, what I'm going
to do is I'm going to say document.getElementById and I'm going to get
the "go-button" I'll show you guys what I mean.
Let's set it to b. So b.text, sorry b.innerText, it's going to say "Go." So this
is very similar to inner HTML, which I was discussing before, the only
difference is that inner text is literally only going to print out text
If there's any HTML code within an element, and I'm going to show you guys
actually right now, and then we'll get on to other stuff. Let me go ahead and put a
bunch of content in here. So I'm going to put a <div>, Hi, and then a
comma and a space. Now here I'm going to create a div with a class of "unnecessary"
and then inside that I'm going to put a <span> and I'm going to say World; Now I'm
going to save this and I'm going to refresh it. Now the divs cause some you know line
breaks here obviously, but this is just for demonstration purposes. So, var t
equals document.getElementById, and we're going to get it by "text" and now
I'm going to print out t.innerText, and it's going to print out just the text,
and then if I go t.innerHTML it's going to print out all of the code that
is inside of that element. So, that's that's the main difference. I think with
text I was thinking of a framework specifically that we work with the
text variable inside of an object, so I apologize for that.
With textareas and inputs it is always the value that is used to grab the actual
value that's inside of that input. So, thank you guys for watching. In the next
video I think we're going to start on objects, classes, and a lot of different
things. Actually, I think we need to do more on functions. So we need to speak
about a few different things regarding a function which we should do in the next
video, and then you know some other awesome stuff after that.
