can anyone help me with some simple javascript please?


this simple thing i'm asking with, (shamefully?) i've never got around learning javascript - never found myself needing/wanting use much! anyway, i've been asked in javascript , have no idea. hugely appreciated.

basic task have number of checkboxes on page (not set number - anywhere between 1 , four) , need them behave radio group; 1 can selected @ 1 time. guess need function which, upon clicking on 1 checkbox, automatically finds others on page , unselects them.

i'm clueless stuff, , google proving fruitless. anyone?
 

if need act radio boxes, why code them check boxes? seems counter-intuitive. it's why google didn't turn much.

anyways though, here's complete solution you. it's not overly simplistic thing, not hard either.

html:
<!doctype html public "-//w3c//dtd html 4.01//en"         "http://www.w3.org/tr/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>radio checkboxes</title> <script type="text/javascript"> var radiocheckboxes = function() {   checkboxes = new array();   clicked = function(elt) {     // uncheck check boxes     (var x=0, y=checkboxes.length; x<y; checkboxes[x].checked=false, x++);     // check 1 clicked     elt.checked = true;   };   init = function() {     // gather input elements     var inputs = document.getelementsbytagname('input');     (var x=0, y=inputs.length; x < y; x++) {       // cycle through looking checkboxes       if (inputs[x].type == 'checkbox') {         // when found, add checkbox array         checkboxes.push(inputs[x]);         // attach event handler being clicked         inputs[x].onclick = function() { clicked(this); };       }     }   };   window.onload = init; // things started once page loads }(); </script> </head> <body> <ul>   <li><input type="checkbox" name="check1" id="check1">   <label for="check1">one</label></li>   <li><input type="checkbox" name="check2" id="check2">   <label for="check2">two</label></li>   <li><input type="checkbox" name="check3" id="check3">   <label for="check3">three</label></li> </ul> </body> </html> 
 


Forums Special Interests Web Design and Development


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

Comments

Popular posts from this blog

Upgrade Eligibility?

need help with itunes 7.7

How to change Apple IDs on iPhones