< Return to Video

Processing forms with events (Video Version)

  • 0:01 - 0:05
    A form is a way for a webpage
    to send information to a server.
  • 0:05 - 0:10
    We don't let the webpages here
    on Khan Academy interact with servers
  • 0:10 - 0:11
    for security reasons.
  • 0:11 - 0:14
    So we don't teach forms here.
  • 0:14 - 0:17
    But, now that you are learning JavaScript
    to manipulate webpages,
  • 0:17 - 0:21
    I can show you how to use JavaScript
    to process form elements
  • 0:21 - 0:23
    instead of having to
    send them to a server.
  • 0:23 - 0:28
    I've set up a bunch of form elements
    on this page to find out the user's name,
  • 0:28 - 0:30
    and their language.
  • 0:30 - 0:34
    And I want the webpage to say hello
    to the user in their language
  • 0:34 - 0:37
    once they click this button.
  • 0:37 - 0:41
    The first step is to store
    the button element in a variable.
  • 0:41 - 0:49
    So we'll just say:
    `document.getElementById("button")`.
  • 0:49 - 0:53
    The next step is to define
    the event listener function.
  • 0:53 - 0:58
    `var onButtonClick = function() {`
  • 0:58 - 1:04
    and then inside of here, we'll just start
    with having it append a message.
  • 1:04 - 1:09
    So,
    `document.getElementById("message")`,
  • 1:09 - 1:11
    we have a nice little message `div`.
  • 1:11 - 1:21
    And we'll just: `textContent +=
    "You clicked me! Thank you so much!"`
  • 1:21 - 1:23
    A very grateful div!
  • 1:23 - 1:29
    Finally, step three, we will
    add the event listener to the button
  • 1:29 - 1:32
    so that it calls that function
    when it's clicked.
  • 1:32 - 1:36
    so, `("click", onButtonClick)`.
  • 1:36 - 1:43
    Pause the talk-through now, and see for
    yourself that it works like you expected.
  • 1:43 - 1:48
    Now, let's make it actually say something
    based on what's in the form.
  • 1:48 - 1:53
    We need to figure out how to get whatever
    the user typed in the name input field.
  • 1:53 - 1:56
    Let's make a variable for it.
  • 1:56 - 2:05
    So,
    `var name = document.getElementById`,
  • 2:05 - 2:07
    since it had an ID,
  • 2:07 - 2:11
    so we've got that there.
  • 2:11 - 2:17
    Let's make a new string for the greeting,
    and concatenate the name of it,
  • 2:17 - 2:23
    so `var greeting = "Heyaz"`,
    that's my favorite greeting,
  • 2:23 - 2:24
    ` + name`.
  • 2:24 - 2:30
    Okay, so we've got a string, plus
    whatever's being stored in that variable.
  • 2:30 - 2:39
    And now, this here, this text content,
    should actually be `greeting`.
  • 2:39 - 2:42
    Let's see, that looks right--
  • 2:42 - 2:45
    we found the name input,
    we made a greeting string,
  • 2:45 - 2:48
    and we appended it to the div.
  • 2:48 - 2:53
    Pause the talk-through,
    and see if it worked.
  • 2:53 - 2:56
    Not quite, right?
  • 2:56 - 3:02
    Did you see "Heyaz [object Object]", or
    "Heyaz object Element"?
  • 3:02 - 3:05
    Assuming that your name isn't Object--
  • 3:05 - 3:08
    and no offense if it is,
    that's a great name--
  • 3:08 - 3:11
    that means that something's wrong.
  • 3:11 - 3:14
    We expected to see whatever you typed in.
  • 3:14 - 3:16
    But we saw "object" instead.
  • 3:16 - 3:21
    That means that the `name` variable
    is pointing at an object,
  • 3:21 - 3:24
    not the string we expected it to point at.
  • 3:24 - 3:27
    Maybe you've already
    figured out the problem.
  • 3:27 - 3:32
    We stored the whole element object
    inside the `name` variable.
  • 3:32 - 3:35
    And the element object is a big object
  • 3:35 - 3:37
    with lots of information
    about the element:
  • 3:37 - 3:40
    all of its attributes, all of its methods.
  • 3:40 - 3:43
    To find out what the user typed in,
    we have to access a particular property
  • 3:43 - 3:46
    of the element: the value.
  • 3:46 - 3:52
    I'll just go ahead and type `.value`,
    and that should fix it.
  • 3:52 - 3:56
    Pause the talk-through, try again.
  • 3:56 - 3:58
    It worked, right?
  • 3:58 - 4:02
    Now that's a common mistake to make,
    so look out for it.
  • 4:02 - 4:05
    I want to show you
    one more common mistake.
  • 4:05 - 4:14
    I'll take this line here, and move it
    outside of the function.
  • 4:14 - 4:23
    Now, pause the talk-through,
    type in the input, and press the button.
  • 4:23 - 4:26
    If it broke the way it should,
    then you should have seen
  • 4:26 - 4:28
    an empty string for your name.
  • 4:28 - 4:30
    Have you figured out why?
  • 4:30 - 4:34
    You have to think carefully about
    when each line of code is executed.
  • 4:34 - 4:37
    In the current code,
    the browser loads the page,
  • 4:37 - 4:40
    and executes the JavaScript line-by-line.
  • 4:40 - 4:43
    First, it stores
    the button element in a variable.
  • 4:43 - 4:47
    Then, it stores the value of
    the input element in the variable.
  • 4:47 - 4:50
    But it stores the value
    at the time that the page loads--
  • 4:50 - 4:52
    which would be empty.
  • 4:52 - 4:56
    Then, it defines a function and
    assigns the event listener.
  • 4:56 - 5:00
    When the listener is called,
    the name is still the same string
  • 5:00 - 5:03
    as it was when the page loaded.
  • 5:03 - 5:06
    So the listener never finds out
    the latest thing that the user typed.
  • 5:06 - 5:09
    That's why this line of code
  • 5:09 - 5:13
    has to be inside
    this event listener function,
  • 5:13 - 5:19
    so that it finds out the value
    at the time that the event happens.
  • 5:19 - 5:22
    Let's add some code to look at
    the language value now,
  • 5:22 - 5:24
    to make sure you're really getting this.
  • 5:24 - 5:30
    Inside the listener, I'll store
    the language in a `lang` variable.
  • 5:32 - 5:35
    Ugh, look at this indenting,
    this is horrible.
  • 5:35 - 5:37
    Let's just line these things up.
  • 5:37 - 5:39
    Okay, so...
  • 5:39 - 5:41
    [typing]
  • 5:47 - 5:51
    And you'll notice that I am naming
    my variables the same thing as my IDs,
  • 5:51 - 5:55
    but that's just what I'm doing,
    you don't have to do that.
  • 5:55 - 6:00
    Now I want to output a different message
    based on what language they picked.
  • 6:00 - 6:04
    Notice the value isn't
    what's shown in the drop-down.
  • 6:04 - 6:07
    It's the value attribute in the HTML.
  • 6:07 - 6:11
    So `lang` should be either
    "en", "es", or "plt".
  • 6:11 - 6:17
    So that means:
    `if (lang === "es")`,
  • 6:17 - 6:24
    then the greeting should be "Hola,".
  • 6:24 - 6:27
    Let's go ahead and make this
    `greeting` variable here.
  • 6:27 - 6:35
    So then `greeting` will be
    "Hola, " plus the name.
  • 6:35 - 6:41
    And then if the lang equals "plt",
    good old Pig Latin,
  • 6:41 - 6:49
    the greeting should be,
    "Ello-hey, " plus the name.
  • 6:49 - 6:53
    and then we can just
    use an `else` for our English.
  • 6:53 - 6:55
    I'll just move this in here.
  • 6:55 - 6:57
    All right.
  • 6:57 - 7:00
    Ooh, and if you want a
    fun bonus challenge,
  • 7:00 - 7:04
    try making a Pig Latin
    converter for names,
  • 7:04 - 7:08
    so that the entire greeting,
    including their name gets translated.
  • 7:08 - 7:10
    That'd be pretty neat.
  • 7:10 - 7:13
    Now pause the talk-through,
    enter your name,
  • 7:13 - 7:19
    pick a different language, and try it out.
  • 7:19 - 7:20
    Neat, huh?
  • 7:20 - 7:25
    Now, there's a lot of stuff you can do
    with form inputs and a little JavaScript:
  • 7:25 - 7:27
    word games, number games,
    story makers...
  • 7:27 - 7:30
    and if you do have a server
    outside of Khan Academy,
  • 7:30 - 7:33
    you can use JavaScript to do
    pre-processing on form input
  • 7:33 - 7:35
    before sending it to a server.
  • 7:35 - 7:39
    There are also a lot more events
    you can listen to on forms,
  • 7:39 - 7:41
    like keypress and focus events.
  • 7:41 - 7:44
    Just remember to look at
    the value of the inputs,
  • 7:44 - 7:47
    and to check that value at the right time.
  • 7:47 - 7:50
    You'll get to practice that
    in the next challenge,
  • 7:50 - 7:53
    which is a personal favorite of mine.
Title:
Processing forms with events (Video Version)
Description:

more » « less
Video Language:
English
Duration:
07:54

English subtitles

Revisions