Section 3.15. Getting the value of a radio group


3.15. Getting the value of a radio group

The key to getting the size and beverage that Jim selected is realizing that there are three different <input> elements, all named "size" (and "beverage"). So you need to figure out which of these three is currently selected:

 function getSize()   {orderCoffee() will call this function to find out what size coffee was ordered. "document" is the entire web page, and forms[0] is the first form on that page. Then, "size" is the name of the <input> elements that let users select their drink size. Since there is more than one element named "size", this variable will be a group of elements, not just a single <input> element.     var sizeGroup = document.forms[0].size;This line loops through all the elements in the "size" grouping... ...and this line checks to see if the current element is checked (selected).     for (i=0; i<sizeGroup.length; i++) {         if (sizeGroup[i].checked == true) {             return sizeGroup[i].value;If we've found the option that is checked, then return the value of that item.         }     } }The returned value will be either "small", "medium", or "large", which is exactly what the orderCoffee() function needs. 

Just Do It

It's your turn to work with radio <input> elements. First, open up coffee.js and add the orderCoffee(), sendRequest(), and getSize() functions that we've looked at over the last few pages.

Next, you need to write the getBeverage() function. You should be able to figure this out from the HTML in coffee.html, and the getSize() function you just looked at. Once you're done, be sure you save coffee.html, and then turn the page.


Espresso Talk

Tonight's overly caffeinated guests:

Asynchronous and Synchronous Application

Synchronous: Hey Mr. Asynchronous, long time no talk.

Asynchronous: No kidding... every time I call you, I get a busy signal. You'd think we weren't even brothers.

Synch: I'm a busy guy, you know? And I don't let anything get in the way of me paying attention to the user I'm serving.

Asynch: I can see that. But what about all your other users, waiting around?

Synch: They'll get their turn, too. Remember, I learned from Client-Server himself. He was really great; everyone liked him just fine, and he was the strong, silent type, just like me.

Asynch: I guess. I always thought he seemed a lot more like the my-users-are-all-tired-of-waiting-onme type.

Synch: You know, you seem to forget who's the older sibling here. I've been around a lot longer than you.

Asynch: Yeah, and so have your users... waiting and waiting...

Synch: Look, nobody gives their users more time and attention than I do. I'm just trying to please them. Meanwhile, you're running around, trying to please everyone. That never works!

Asynch: It sounds to me like you're trying to please your USER... like just one. Everybody else has to wait in line.

Synch: Hey, sometimes it's much better to take care of one thing at a time, and then move on to other jobs.

Asynch: Sure, maybe if someone submits like 200 pieces of data. But most of the time, you're just not needed anymore.

Synch: Just because I don't let people interrupt me while I'm working...

Asynch: Hey, I can listen and talk, all at the same time. You're the one with a one-track mind...

Synch: One-track mind? I just make sure I finish what I start.

Asynch: Sure, but what if that takes ten seconds? Or ten minutes? Or an hour? Come on, do you really think people enjoy that little hourglass whirling around?

Synch: I don't seem to get many complaints.

Asynch: Yeah, well, I'd love to sit around like this all day, but my users don't like to wait on me. That's more your department.

Synch: Yeah, enjoy your 15 minutes, bro. I've seen fads like you come and go a million times.

Asynch: I bet you thought U2 was a one-hit wonder, too. I'm not going anywhereexcept to make the web a hip place again. See you when I see you, Synch...





Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net