{"id":65788,"date":"2021-07-14T04:10:06","date_gmt":"2021-07-14T08:10:06","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/handling-checkbox-arrays-with-javascriptgetelementsbynamexhtml-supported-javascript\/"},"modified":"2021-07-14T04:10:06","modified_gmt":"2021-07-14T08:10:06","slug":"handling-checkbox-arrays-with-javascriptgetelementsbynamexhtml-supported-javascript","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=65788","title":{"rendered":"Handling Checkbox arrays with Javascript::GetElementsByName:XHTML supported #JavaScript"},"content":{"rendered":"<p>The following code demonstrates how to handle checkboxes with JavaScript. Note the names of all c heck boxes are same myInput[]. It could also be myInput. Both works as an array to contain the controls&#8217; values. I prefer myInput[] as you can handle this control in PHP as an array. If you use $arr=$_POST[&#8216;myInput&#8217;], $arr will contain the values of the controls.<\/p>\n<p>Note the use of getElementsByName(). It is easy to manipulate JavaScript controls using this name. You do not need to send a value\/control to the function. And in many times, document.myform.control, does not work right, specially if you want to main XHTML standard[i need to check more on this]. getElementById is also very useful. You may use getElementById for most times, but with checkbox arrays getElementsByName is better choice.<\/p>\n<p>Also, note that when a form is submitted only the values of the checked check boxes are submitted to the server. You may wish to use hidden controls to keep the values, so that all values are submitted and based on the checked\/non-checked you can ..control your work<\/p>\n<pre>&lt;html&gt;\r\n\t&lt;head&gt;\r\n\t\t&lt;script type=\"text\/javascript\"&gt;\r\n\t\t\tfunction getElements()\r\n\t\t\t  {\r\n\t\t\t  var x=document.getElementsByName(\"myInput[]\");\r\n\t\t\t  alert(x.length);\r\n\t\t\t  alert(x[0].value);\r\n\t\t\t  alert(x[1].value);\r\n\t\t\t  alert(x[2].value);\r\n\r\n\r\n\t\t\t  }\r\n\t\t&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\t&lt;input name=\"myInput[]\" type=\"checkbox\" size=\"20\" value='10'\/&gt;10 \r\n\t&lt;input name=\"myInput[]\" type=\"checkbox\" size=\"20\" value='20' \/&gt;20\r\n\t&lt;input name=\"myInput[]\" type=\"checkbox\" size=\"20\" value='30' \/&gt;30<\/pre>\n<p> From: http:\/\/sitestree.com\/?p=3471<br \/> Categories:JavaScript<br \/>Tags:<br \/> Post Data:2016-07-09 20:37:45<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following code demonstrates how to handle checkboxes with JavaScript. Note the names of all c heck boxes are same myInput[]. It could also be myInput. Both works as an array to contain the controls&#8217; values. I prefer myInput[] as you can handle this control in PHP as an array. If you use $arr=$_POST[&#8216;myInput&#8217;], $arr &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=65788\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-65788","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":70125,"url":"http:\/\/bangla.sitestree.com\/?p=70125","url_meta":{"origin":65788,"position":0},"title":"Handling Checkbox arrays with Javascript::GetElementsByName:XHTML supported #13","author":"Author-Check- Article-or-Video","date":"August 26, 2021","format":false,"excerpt":"The following code demonstrates how to handle checkboxes with javascript. Note the names of all check boxes are same myInput[]. It could also be myInput. Both works as an array to contain the controls' values. I prefer myInput[] as you can handle this control in PHP as an array. If\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10562,"url":"http:\/\/bangla.sitestree.com\/?p=10562","url_meta":{"origin":65788,"position":1},"title":"GetElementsByName an example: Checkbox","author":"","date":"August 29, 2015","format":false,"excerpt":"<html> \u00a0\u00a0 \u00a0<head> \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<script type=\"text\/javascript\"> \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0function getElements() \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 { \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 var x=document.getElementsByName(\"myInput[]\"); \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 alert(x.length); \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 alert(x[0].value); \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 alert(x[1].value); \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 alert(x[2].value); \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 } \u00a0\u00a0 \u00a0\u00a0\u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26495,"url":"http:\/\/bangla.sitestree.com\/?p=26495","url_meta":{"origin":65788,"position":2},"title":"GetElementsByName an example: Checkbox #Programming Code Examples #Javascript #JavaScript","author":"Author-Check- Article-or-Video","date":"April 26, 2021","format":false,"excerpt":"<html> <head> <script type=\"text\/javascript\"> function getElements() { var x=document.getElementsByName(\"myInput[]\"); alert(x.length); alert(x[0].value); alert(x[1].value); alert(x[2].value); } <\/script> <\/head> <body> <input name=\"myInput[]\" type=\"checkbox\" size=\"20\" value='10'\/>10 <input name=\"myInput[]\" type=\"checkbox\" size=\"20\" value='20' \/>20 <input name=\"myInput[]\" type=\"checkbox\" size=\"20\" value='30' \/>30 <br \/> <input type=\"button\" onclick=\"getElements()\" value=\"How many elements named 'myInput'?\" \/> <\/body> <\/html> Note: Brought from\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":70149,"url":"http:\/\/bangla.sitestree.com\/?p=70149","url_meta":{"origin":65788,"position":3},"title":"JavaScript Miscellaneous Topics #13","author":"Author-Check- Article-or-Video","date":"August 27, 2021","format":false,"excerpt":"JavaScript Miscellaneous Topics. Please check below. Detect browser name and version: var browser=navigator.appName; var b_version=navigator.appVersion; Other browser related: navigator.appCodeName, navigator.platform, navigator.platform , navigator.cookieEnabled , navigator.userAgent, navigator.appMinorVersion, navigator.cpuClass, navigator.onLine, navigator.browserLanguage, navigator.systemLanguage, navigator.userLanguage It's a good idea to use escape function for setting cookie values (if more than one word). Also, use\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"hello","src":"b_pink.gif","width":350,"height":200},"classes":[]},{"id":70151,"url":"http:\/\/bangla.sitestree.com\/?p=70151","url_meta":{"origin":65788,"position":4},"title":"JavaScript Short-notes: #13","author":"Author-Check- Article-or-Video","date":"August 27, 2021","format":false,"excerpt":"Ajax: An OverviewHandling Checkbox arrays with Javascript::GetElementsByName:XHTML supportedHow to implement multiple tab webpages in Ajax and JavascriptJavascript - Form validationJavaScript - Miscellaneous ValidationsJavascript by ExampleJavascript CodesJavaScript DOM: Must knowledge to understand AjaxJavaScript FrameworksJavaScript Miscellaneous TopicsJavaScript: Pass by Value: Pass by ReferenceRandom Notes on JavaScript\/HTMLWeb page design standardsWhat you really need\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24402,"url":"http:\/\/bangla.sitestree.com\/?p=24402","url_meta":{"origin":65788,"position":5},"title":"JavaScript DOM: Must knowledge to understand Ajax #Root","author":"Author-Check- Article-or-Video","date":"April 8, 2021","format":false,"excerpt":"Understanding Javascript DOM is the first step to understand Ajax. DO M provides many methods to access and edit individual components of a web-page\/document. The methods are called getters. < div id = 'test' > Hello < \/div > getElementById method can reference to this div element. var testDiv =\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/65788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=65788"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/65788\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=65788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=65788"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=65788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}