search
HomeWeb Front-endJS TutorialJavaScript Beginner Tutorial (Lesson 5 Continued)_Basic Knowledge

The usage of radio buttons in JavaScript is similar to that of check boxes. The difference lies in the application in HTML. A checkbox is a switch. If a checkbox is selected, you can click it again to deselect it. But if a radio button is selected, you can only deselect it by selecting another radio button. Example:

In this example, if you want to deselect a radio button, you must click on another radio button. Look at the following program again:


Light off
Light on
 

  When the first radio button box is selected, the function offButton() is called . The function is as follows:

function offButton()
{
var the_box = window.document.form_1.radio_1;

if (the_box.checked == true)
{
           window.document.form_1.radio_2.checked = false;

This example is very similar to the previous checkbox example. The main difference is this line:

window.document.form_1.radio_2.checked = false;

This line Directive instructs JavaScript to close another button when this button is clicked. Since the function of another button is very similar to this one:

function onButton()

{

var the_box = window.document.form_1.radio_2;

if (the_box.checked == true)
{
window.document.form_1.radio_1.checked = false; }

}








Menu is the most peculiar form option we have learned. There are two basic formats: following menus and list menus. The following is an example:

The strange thing is that this menu has a name, but the options in it do not have names. For example, in HTML, the first menu is as follows:

< ;option>spider

;

>


Note that the name of this menu is pulldown_1, but each option has no name. So it's a bit difficult to call each of the options.

Fortunately, arrays can help us call the options. If you want to change the second option in the following menu, you can do this:

window.document.form_1.pulldown_1.options[1].text = 'new_text';

This is because the menu element has an options attribute, which is an array of all options in the menu. Click change the selectt and then use the drop-down menu to see if its selection has been changed. Now the 2nd option should be *thau*.

In addition to the option attribute, the menu also has an attribute called selectedIndex. After an option is selected, the selectedIndex attribute will become the array index number (serial number) of the selected option. Select an option in the second list menu and check the index number. Remember that the first option in the array has index 0.

check the index.

The name of the form is form_1, and the name of the list menu is list_1. The selectedIndex attribute value is window.document.form_1.list_1.selectedIndex. You can also

set the selectedIndex as follows:

window.document.form_1.list_1.selectedIndex = 1;

and highlight the second option.

Once you get the index number of the selected item, you can find its content:


var the_select = window.document.form_1.list_1;

var the_index = the_select. selectedIndex;

var the_selected = the_select.options[the_index].text;

The selectedIndex attribute is useful, but what happens if multiple options are selected at the same time?

The handler of the menu element is onChange(). When the menu changes, the processor is activated.



Try this example and read the comments below:

My favorite animal is...

Comment a relatively complex JavaScript program. First, let’s look at the form itself:


selectedIndex].text);">
                                             " option>
                                  ;/select>

🎜>                                                                             ;greyhound                                                                                                                                                                            The handler for the following menus calls the function swapOptions(). This function has been defined in the header

, and its parameter is - the selected animal type.

The first few arrays I defined in the header:

var dogs = new Array("poodle","puli","greyhound");

var fish = new Array("trout", "mackerel", "bass");


var birds = new Array("robin", "hummingbird", "crow");

Pay attention to these arrays The naming is consistent with the naming in the drop-down menu. Soon you'll understand why. Now let’s look at the function that is called when the drop-down menu is changed:

function swapOptions(the_array_name)

{

var numbers_select = window.document.the_form.the_examples;

var the_array = eval(the_array_name);

setOptionText(window.document.the_form.the_examples, the_array);

}

The definition of this function includes one parameter: the_array_name. If you open the drop-down menu and select "Fish", the_array_name is equivalent to the string "Fish".


The first line in the function body includes a variable to reference the second form element: the list menu.

Line 2 introduces a new concept: eval(). eval() is rather strange, we will explain it in a later course. The result of these commands on line 2 is that the variable the_array will be equal to one of the arrays defined previously. If the_array_name is "Fish", the_array is equivalent to the Fish array. If you want to understand how this is done, learn eval.

Line 3 defines another function setOptionText(). setOptionText() is used to assign the_array to the list menu. The following is the function content:

function setOptionText(the_select, the_array)

{

for (loop=0; loop {

the_select.options[loop].text = the_array[loop];

}

}

This function takes two parameters: a reference to the menu element and an array. Line 1 sets up a for loop to loop through all menu elements. Note that the options attribute of the menu element is an array consisting of all the options of the menu. Because it is an array, you can find the number of elements in the array using the length property. During the first loop, the loop variable value is 0. The main value of the loop is:

the_select.options[0].text = the_array[0];

If you select "Fish" in the drop-down menu, then the_array[0] is "trout", so this line of command changes the first option in the list menu to "trout". The next time it loops, the loop is equal to 1, and the second option in the list menu is "mackerel" .

If you understand this example, you have a deeper understanding of JavaScript.

This is the end of the basic tutorial, and advanced tutorials will be released later, so stay tuned.



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.