Tip:
Highlight text to annotate it
X
>> DAVID J. MALAN: Now, in that last example, I interspersed my JavaScript
code within my HTML, specifically putting that call to quote inside of
the value of my on submit attribute.
Let's clean things up a bit and factor out all of that JavaScript code into a
script tag in my pages head, and instead give that same form tag a
unique identifier via which we can get it.
>> Specifically, let's replace this attribute here with ID equals quote,
unquote, say, quote.
Meanwhile, let's go up to my existing quote function, cut out that code
altogether, and replace the function quote itself with the following--
dollar sign, function, specifying that when the document is ready using
jQuery shorthand notation, I want to execute the following code.
Dollar sign, quote, unquote, quote--
no pun intended--
thereby specifying that I'd like to get the element from my DOM, whose
unique identifier is quote, and then I'd like to register an event handler
for the form submission with dot submit, passing to that another
anonymous function that this time will take one argument, an event.
And then, inside of this function I'm going to paste that code from before
but specify that I'd like to prevent the default behavior of that
submission event so that the form itself is not submitted to an actual
remote server.
>> Let's now save this file and open it up in my browser.
http://localhost/ajax.html.
There's that form.
Let's type in a symbol like FB for Facebook and get quote.
And there's Facebook's latest stock price.