{"id":380,"date":"2011-12-20T14:23:33","date_gmt":"2011-12-20T14:23:33","guid":{"rendered":"http:\/\/javascript-tutor.net\/index.php\/"},"modified":"2017-10-17T00:15:51","modified_gmt":"2017-10-17T00:15:51","slug":"lesson-19-radio-button-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/","title":{"rendered":"JavaScript Lesson 19: The Radio Button"},"content":{"rendered":"<h4 align=\"center\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-18-events-javascript-part-4\/\">&lt;Lesson 18&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/tutorial\/\">[Table of Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-20-checkboxes-javascript\/\">&lt;Lesson 20&gt;<\/a><\/strong><\/h4>\n<p>In this lesson, we shall learn\u00a0how to write code for radio buttons in JavaScript. The function of the radio buttons is to let the user select one item from a group of items. Radio buttons does not allow the user to select more than one item. There should be at least two radio buttons on a form. Once the user clicks on a radio button, he or she cannot unclick it.<\/p>\n<h3><strong>19.1 Using the radio button<\/strong><\/h3>\n<p>In this example, the user is presented with four types of fruits and he or she can choose only one type at one time by clicking the radio button beside the label of a particular fruit. The code is as follow:<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4><strong>Example 19.1: Code for Item Selection<\/strong><\/h4>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=windows-1252\"&gt;\r\n&lt;title&gt;JavaScript Example 19.2&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\nfunction checkItem()\r\n{for(count=0;count&lt;4;count++)\r\n{if (MyForm.item[count].checked)\r\nalert(MyForm.item[count].value+\" is selected\");\r\n}\r\n}\r\n&lt;\/script&gt;\r\n&lt;form&gt;\r\n&lt;input value=\"Select and Click\" onclick=\"checkItem()\"&gt;&lt;br&gt;\r\n&lt;input value=\"Apple\"&gt;\r\n&lt;label&gt;Apple&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Orange\"&gt;\r\n&lt;label&gt;Orange&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Grape\"&gt;\r\n&lt;label&gt;Grape&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Melon\"&gt;\r\n&lt;label&gt;Melon&lt;\/label&gt;&lt;br&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<p>Click\u00a0<a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example19.1.htm\">Example 19.1<\/a> to try it out.<\/p>\n<h4><b>Example 19.2: Load Image of Choice<\/b><\/h4>\n<p>You can modify the above program by asking the user to select an image of his or her choice. Here I have designed six images representing the faces of a dice.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif\" alt=\"\" \/><\/p>\n<pre>&lt;html&gt;&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=windows-1252\"&gt;\r\n&lt;title&gt;JavaScript Example 19.2&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\nfunction checkItem()\r\n{for(count=0;count&lt;6;count++)\r\n{if (MyForm.item[count].checked)\r\ndocument.write ( \"&lt;img src = \\\"\" +\r\n(Math.round(count)+1)+\r\n\".jpg\\\" width = \\\"50\\\" height = \\\"50\\\" \/&gt;\");\r\n\r\n}\r\n}\r\n&lt;\/script&gt;\r\n&lt;form&gt;\r\n&lt;input value=\"Select and Click\" onclick=\"checkItem()\"&gt;&lt;br&gt;\r\n&lt;input value=\"One\"&gt;\r\n&lt;label&gt;One Dot&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Two\"&gt;\r\n&lt;label&gt;Two Dots&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Three\"&gt;\r\n&lt;label&gt;Three Dots&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Four\"&gt;\r\n&lt;label&gt;Four Dots&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Five\"&gt;\r\n&lt;label&gt;Five Dots&lt;\/label&gt;&lt;br&gt;\r\n&lt;input value=\"Six\"&gt;\r\n&lt;label&gt;Six Dots&lt;\/label&gt;&lt;br&gt;\r\n\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<p>Please click on\u00a0<a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example19.2.htm\">Example 19.2<\/a> to test the output.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\" align=\":center&quot;\"><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-18-events-javascript-part-4\/\">&lt;Lesson 18&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-20-checkboxes-javascript\/\">&lt;Lesson 20&gt;<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Lesson 18&gt; [Table of Contents] &lt;Lesson 20&gt; In this lesson, we shall learn\u00a0how to write code for radio buttons in JavaScript. The function of the radio buttons is to let the user select one item from a group of items. Radio buttons does not allow the user to select more than one item. There should &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 19: The Radio Button&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-380","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 19: The Radio Button - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"description\" content=\"This javascript lesson illustrates the use of radio buttons in javascript\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 19: The Radio Button - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"This javascript lesson illustrates the use of radio buttons in javascript\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-10-17T00:15:51+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/\",\"name\":\"JavaScript Lesson 19: The Radio Button - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif\",\"datePublished\":\"2011-12-20T14:23:33+00:00\",\"dateModified\":\"2017-10-17T00:15:51+00:00\",\"description\":\"This javascript lesson illustrates the use of radio buttons in javascript\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#primaryimage\",\"url\":\"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif\",\"contentUrl\":\"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 19: The Radio Button\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 19: The Radio Button - Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"This javascript lesson illustrates the use of radio buttons in javascript","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 19: The Radio Button - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"This javascript lesson illustrates the use of radio buttons in javascript","og_url":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-10-17T00:15:51+00:00","og_image":[{"url":"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/","url":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/","name":"JavaScript Lesson 19: The Radio Button - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#primaryimage"},"image":{"@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#primaryimage"},"thumbnailUrl":"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif","datePublished":"2011-12-20T14:23:33+00:00","dateModified":"2017-10-17T00:15:51+00:00","description":"This javascript lesson illustrates the use of radio buttons in javascript","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#primaryimage","url":"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif","contentUrl":"http:\/\/javascript-tutor.net\/java_tutor\/image\/figure10.6.gif"},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-19-radio-button-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 19: The Radio Button"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=380"}],"version-history":[{"count":14,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/380\/revisions"}],"predecessor-version":[{"id":2397,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/380\/revisions\/2397"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}