1 00:00:00,000 --> 00:00:00,360 2 00:00:00,360 --> 00:00:03,140 >> DAVID J. MALAN: Now, in that last example, I interspersed my JavaScript 3 00:00:03,140 --> 00:00:07,840 code within my HTML, specifically putting that call to quote inside of 4 00:00:07,840 --> 00:00:10,190 the value of my on submit attribute. 5 00:00:10,190 --> 00:00:13,820 Let's clean things up a bit and factor out all of that JavaScript code into a 6 00:00:13,820 --> 00:00:18,110 script tag in my pages head, and instead give that same form tag a 7 00:00:18,110 --> 00:00:21,320 unique identifier via which we can get it. 8 00:00:21,320 --> 00:00:26,800 >> Specifically, let's replace this attribute here with ID equals quote, 9 00:00:26,800 --> 00:00:29,030 unquote, say, quote. 10 00:00:29,030 --> 00:00:33,420 Meanwhile, let's go up to my existing quote function, cut out that code 11 00:00:33,420 --> 00:00:37,860 altogether, and replace the function quote itself with the following-- 12 00:00:37,860 --> 00:00:42,850 dollar sign, function, specifying that when the document is ready using 13 00:00:42,850 --> 00:00:48,160 jQuery shorthand notation, I want to execute the following code. 14 00:00:48,160 --> 00:00:51,120 Dollar sign, quote, unquote, quote-- 15 00:00:51,120 --> 00:00:52,360 no pun intended-- 16 00:00:52,360 --> 00:00:56,120 thereby specifying that I'd like to get the element from my DOM, whose 17 00:00:56,120 --> 00:01:01,340 unique identifier is quote, and then I'd like to register an event handler 18 00:01:01,340 --> 00:01:05,459 for the form submission with dot submit, passing to that another 19 00:01:05,459 --> 00:01:09,750 anonymous function that this time will take one argument, an event. 20 00:01:09,750 --> 00:01:13,390 And then, inside of this function I'm going to paste that code from before 21 00:01:13,390 --> 00:01:17,840 but specify that I'd like to prevent the default behavior of that 22 00:01:17,840 --> 00:01:21,390 submission event so that the form itself is not submitted to an actual 23 00:01:21,390 --> 00:01:22,450 remote server. 24 00:01:22,450 --> 00:01:24,725 >> Let's now save this file and open it up in my browser. 25 00:01:24,725 --> 00:01:27,630 26 00:01:27,630 --> 00:01:34,720 http://localhost/ajax.html. 27 00:01:34,720 --> 00:01:35,760 There's that form. 28 00:01:35,760 --> 00:01:39,590 Let's type in a symbol like FB for Facebook and get quote. 29 00:01:39,590 --> 00:01:41,530 And there's Facebook's latest stock price. 30 00:01:41,530 --> 00:01:44,363