< Return to Video

Creating elements from scratch | Computer Programming | Khan Academy

  • 0:01 - 0:03
    - [Voiceover] Now I'm going
    to walk you through the basics
  • 0:03 - 0:06
    of using jQuery in your web page.
  • 0:06 - 0:09
    The first step is to actually
    include the jQuery library
  • 0:09 - 0:12
    using a '<script>' tag
  • 0:12 - 0:16
    Now before, we put JavaScript
    inside of our '' tag,
  • 0:16 - 0:20
    but this time, I'm going
    to add a 'src' attribute.
  • 0:20 - 0:24
    And I need to set that to a URL.
  • 0:24 - 0:27
    What should that URL be?
  • 0:27 - 0:29
    If I was working on my own computer
  • 0:29 - 0:30
    and have downloaded jQuery
  • 0:30 - 0:33
    into the same folder as my web page,
  • 0:33 - 0:36
    I could just write 'jquery.js' here.
  • 0:36 - 0:39
    But that won't work here on Khan Academy.
  • 0:39 - 0:43
    Here, I need an absolute URL of jQuery
  • 0:43 - 0:46
    on an online server somewhere.
  • 0:46 - 0:50
    And there is a list of
    those URLs in jquery.com,
  • 0:51 - 0:53
    and I have one of them
    here I'll just paste in.
  • 0:53 - 0:54
    There we go.
  • 0:54 - 0:56
    All right. Now I wanna
    point out a few things
  • 0:56 - 0:58
    about this URL.
  • 0:58 - 1:02
    The first thing is that
    it starts with 'https'.
  • 1:02 - 1:05
    That means that it is a secure URL.
  • 1:05 - 1:09
    We only allow you to
    bring in secure resources
  • 1:09 - 1:11
    in Khan Academy web pages
  • 1:11 - 1:15
    and that's generally a good
    practice in web development.
  • 1:16 - 1:19
    Okay. The second thing
    is that this is located
  • 1:19 - 1:23
    on a Google server: googleapis.com.
  • 1:23 - 1:25
    This server is called a CDN,
  • 1:25 - 1:28
    a content delivery network,
  • 1:28 - 1:29
    which means that it's optimized
  • 1:29 - 1:33
    for serving static files
    like JavaScript libraries
  • 1:33 - 1:35
    and serving them very quickly.
  • 1:35 - 1:38
    Now this Google server is one that I trust
  • 1:38 - 1:41
    and generally, you should
    always trust the servers
  • 1:41 - 1:43
    you bring scripts in from
  • 1:43 - 1:47
    since they could potentially
    do nasty things to your page.
  • 1:47 - 1:50
    Okay. The third thing is
    that there's a version number
  • 1:50 - 1:54
    in this URL: 2.1.4.
  • 1:54 - 1:56
    The jQuery library is
    in active development
  • 1:56 - 1:59
    and they often release new versions.
  • 1:59 - 2:01
    The numbers farther to the left
  • 2:01 - 2:03
    represent major version changes
  • 2:03 - 2:06
    versus the numbers farther to the right,
  • 2:06 - 2:08
    which represent minor versions.
  • 2:08 - 2:10
    I'm using jQuery 2 here,
  • 2:10 - 2:12
    which works in modern browsers
  • 2:12 - 2:14
    but doesn't work in IE8.
  • 2:14 - 2:16
    On your own site,
  • 2:16 - 2:20
    you can decide which version
    to use based on what you need.
  • 2:20 - 2:23
    All right. So we have jQuery included.
  • 2:23 - 2:25
    Now, let's actually use it
  • 2:25 - 2:28
    inside another '<script>' tag.
  • 2:30 - 2:33
    Every JavaScript library exposes functions
  • 2:33 - 2:35
    and they give their functions names.
  • 2:35 - 2:37
    We have to look in the documentation
  • 2:37 - 2:39
    to find out what those function names are
  • 2:39 - 2:42
    and what they actually do.
  • 2:42 - 2:45
    Well the jQuery library
    exposes one main function
  • 2:45 - 2:48
    and it has a very short name.
  • 2:48 - 2:51
    It's just the dollar sign.
  • 2:51 - 2:52
    That means that the first thing we write
  • 2:52 - 2:54
    is dollar
  • 2:54 - 2:57
    then two parenthesis,
    because it's a function,
  • 2:57 - 2:59
    and of course, semicolon.
  • 2:59 - 3:02
    Now it's nice that this
    function name is so short
  • 3:02 - 3:06
    because we'd be calling
    that function an awful lot.
  • 3:06 - 3:09
    There are lots of things we
    can pass to that function,
  • 3:09 - 3:10
    but for this intro example,
  • 3:10 - 3:15
    I'm just going to pass a string 'h1'.
  • 3:16 - 3:18
    Now, when I've done this,
  • 3:18 - 3:20
    this tells jQuery to find
  • 3:20 - 3:23
    all of the 'h1' elements on the page
  • 3:23 - 3:28
    and return them as a
    jQuery collection object.
  • 3:28 - 3:29
    Now that I've done that,
  • 3:29 - 3:33
    I can call other methods
    on the jQuery object
  • 3:33 - 3:37
    in order to manipulate all the
    'h1' elements that it found.
  • 3:37 - 3:40
    Like if I want to change the
    inner text of all of them,
  • 3:40 - 3:43
    I can call the 'text' function
  • 3:43 - 3:46
    then pass it a string.
  • 3:47 - 3:48
    It's happening.
  • 3:48 - 3:50
    Yes!
  • 3:50 - 3:51
    And we're done!
  • 3:51 - 3:53
    That is our first bit of jQuery.
  • 3:53 - 3:57
    So we included the jQuery
    library with the '' tag,
  • 3:57 - 4:00
    told jQuery to find all
    the 'h1s' on the page,
  • 4:00 - 4:04
    and then asked jQuery to
    change the text of all of them.
  • 4:04 - 4:06
    If you keep going, we'll
    go through much more
  • 4:06 - 4:08
    in more detail,
  • 4:08 - 4:11
    more ways to select and
    manipulate elements,
  • 4:11 - 4:14
    using jQuery to respond to
    user events on the page,
  • 4:14 - 4:19
    plus fun stuff with animation and effects.
Title:
Creating elements from scratch | Computer Programming | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
04:19

English subtitles

Revisions