JavaScript - Get Radio Button Value?
I don't exactly know how to word my issue.
Basically, I need it so that if a user selects 'option1' in the group 'group1' when they go to 'group2' is only gives them a certain selection to choose from. If they choose 'option2' in 'group1' they will get a different set to choose from. My main dilemma is it must do this without them having to submit. Similar TutorialsI have 4 rows in a table. Each row consist of 4 radio buttons. Each radio button per row has different values in it but has the same name (group) ex: <input type="radio" name="a" value="1"> <input type="radio" name="a" value="2"> <input type="radio" name="a" value="3"> <input type="radio" name="a" value="4"> <input type="radio" name="b" value="1"> <input type="radio" name="b" value="2"> <input type="radio" name="b" value="3"> <input type="radio" name="b" value="4"> and so on.. If I click radio button A with value 2, I want to output the total at the bottom as "2".. Also, if I click radio button B with value 3, I want to output the total of A and B as 5 and so on.. How can I automatically calculate the answer based on which radio button was click? update: I got my answer from this site: http://stackoverflow.com/questions/1...s-using-jquery All: I need some help with a radio button code. It seems simple but I am really new at JavaScript so I need help. I have created a JavaScript code that has three radio buttons (High, Medium, and Low). When you click on high two text boxes appear so that an email address can be added. What I need help with is making the text boxes disappear if someone accidentally click high and then went back to low or medium. Currently the High button stays clicked and the text boxes still show. Any help would be great. Thanks, Stephen Code: <script type="text/javascript"> function showHide(el){ obj = document.getElementById(el).style; (obj.display == 'none')? obj.display = 'block' : obj.display = 'none'; } </script> <form id="form1" name="form1" method="post" action=""> <label> <input type="Checkbox" name="High" value="High" onclick="showHide('group1')" /> High </label> <label> <input type="Checkbox" name="Medium" value="Button" /> Mediun </label> <label> <input type="Checkbox" name="Medium" value="Button" /> Low </label> <p id="group1" style="display: none;"> <label> <b>friend1  </b><input type="Text" name="textGroup1" value="@gmail.com" /> <br/> </label> <br /> <label> <b>friend2  </b><input type="text" name="textGroup1" value="@gmail.com" /> </label> </p> </form> All, I have the following code: Code: <form action="" name="myform" method="post"> <input type="hidden" id="picimages" name="picimages" value="<?php echo $resultsetstory['bigger_id']; ?>" /> <p> <label> <input type="radio" id="picimages" name="picimages" value="<?php echo $resultsetstory['picture_id']; ?>" /> <?php if($resultsetstory['title']<>""){ echo $resultsetstory['title']; }else{ echo "Image 1"; } ?> </label> <br /> <label> <input type="radio" id="picimages" name="picimages" value="<?php echo $resultsetpic2['picture_id']; ?>" /> <?php if($resultsetpic2['title']<>""){ echo $resultsetpic2['title']; }else{ echo "Image 2"; } ?> </label> <br /> <input name="submit" type="button" value="Vote!" onclick="javascript:voteupdate(this.myform);"/> </p> </form> When I submit this, it goes to this function: Code: function voteupdate(obj) { var poststr = "picimage=" + document.getElementById("picimage").value + "&bigger_id=" + encodeURI( document.getElementById("bigger_id").value ); alert(poststr); makePOSTRequest('voteresults.php', poststr); } However I can't get the value of the radio group for the one that is selected. Can someone see what is wrong? Thanks. I'm trying to implement a visibility on or off function for extra inputs when a radio button that is "yes" inside a table... it's so "Are you married?" "Yes" "No" (no is default checked) when you hit yes I want 3 more rows like a collapse or extend feature...within the same table. So basically what I want is the table to grow.. If you select "yes" then 3 more rows will be displayed that have other inputs available... do I need to construct these extra rows in javascript? or can I referance some html to do something? ..I'm just kinda lost and brain farted out today I think... any help much appreciated. Thanks! Wondering how come this doesn't work any ideas? its validating that the radio buttons have been selected. Code: if ( ( form.q1[0].checked == false ) && ( form.q2[1].checked == false) && ( form.q3[2].checked == false )) { alert ( "Please Choose a " ); return false; } Hello Friends, i have a small problem and need your help. I have a form with 4 radio buttons, the scenario is, when a user click on a radio button a hidden content will be displayed and the other three radio buttons will be disabled so that at one time a user can only click and view single radio button and the hidden content. Here is my code to show hide the hidden content but i need when the user select one radio button the other radio buttons should disable. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>test</title> <style type="text/css" media="screen"> .hide{ display:none; } </style> </head> <body> <div id="tabs"> <div id="nav"> <p>Show Div 1:<input type="radio" name="tab" value="value1" class="div1" /></p> <p>Show Div 2:<input type="radio" name="tab" value="value2" class="div2" /></p> <p>Show Div 3:<input type="radio" name="tab" value="value3" class="div3" /></p> <p>Show Div 4:<input type="radio" name="tab" value="value4" class="div4" /></p> </div> <div id="div1" class="tab"> <p>this is div 1</p> </div> <div id="div2" class="tab"> <p>this is div 2</p> </div> <div id="div3" class="tab"> <p>this is div 3</p> </div> <div id="div4" class="tab"> <p>this is div 4</p> </div> </div> <script type="text/javascript" charset="utf-8"> (function(){ var tabs =document.getElementById('tabs'); var nav = tabs.getElementsByTagName('input'); /* * Hide all tabs */ function hideTabs(){ var tab = tabs.getElementsByTagName('div'); for(var i=0;i<=nav.length;i++){ if(tab[i].className == 'tab'){ tab[i].className = tab[i].className + ' hide'; } } } /* * Show the clicked tab */ function showTab(tab){ document.getElementById(tab).className = 'tab' } hideTabs(); /* hide tabs on load */ /* * Add click events */ for(var i=0;i<nav.length;i++){ nav[i].onclick = function(){ hideTabs(); showTab(this.className); } } })(); </script> </body> </html> Thank you for your help in advance. I using the following to read the content of a text field but how do I read the content of a radio button ? Code: String.prototype.trim = function () { //return this.replace(/^\s*/, "").replace(/\s*$/, ""); return this.replace(/^\s+|\s+$/g, '').replace(/\s+/g,' '); } var fID = document.getElementById('customerName'); fID.value = fID.value.trim(); if (fID.value.trim().split(' ').length < 2) { document.getElementById('customerNameImg').innerHTML = redX;// } else { document.getElementById('customerNameImg').innerHTML = greenTick;// } this is the part of my form code that i am having problems with Code: <ul> <li class="<? if ($empty_Field == "previousClient") {?>emptyfield<? } else { ?>field<? } ?>"> Are you a Previous Client * </li> <li class="value"> <div style="float: left;"><input type="radio" name="previousClient" id="previousClientY" value="Yes" <? if($previousClient == 'Yes') { echo 'checked'; } ?> onChange="checkValidFormInput();"> Yes </div> <div style="float: left; padding-left: 20px;"> <input type="radio" name="previousClient" id="previousClientN" value="No" <? if($previousClient == 'No') { echo 'checked'; } ?> onChange="checkValidFormInput();"> No </div> <div style="float: left; padding-left: 10px;" id="previousClientImg"></div><br style="clear:both"> </li> </ul> hi guys im trying to check whether a radio button is checked when i click it, but the javascript onlick event is not acting cool! when i click an unselected radio button i am informed that the unselected radio button i just clicked is in fact checked, when i should be getting the information the no, the radio button was not checked when i clicked it. any idea how to get the previous state of a radio button onclick? Hi everyone. I'm getting into javascript coding and i'm stumped with some problem about radio buttons. What I would like is to have a general function where I can pass in any group of radio buttons and have it return the textual value of the radio button selected. First, here is my code so far. Code: function Get_Radio_Selection(options) { var choice = ""; for (var index = 0; index <= options.length; index++) if (options[index].checked == true) choice = options[index].value; return choice; } Now here's a sample form: Code: <form name="form1"> <input type="radio" name="food" value="Banana">Banana <input type="radio" name="food" value="Mango">Mango <input type="radio" name="food" value="Orange">Orange <input type="radio" name="food" value="Oatmeal">Oatmeal <input type="button" name="blah" value="Get Selection" onclick="Get_Radio_Selection(document.form1.food);"> </form> My question is, when i run this function and pass it in the group of radio buttons by means of the onclick event handler, firefox 3.6.22 reports in the error console on line 22 that "options[index] is undefined". It seems that if i use the variable "index" more than one time it reports this error but if I take out the assignment to variable "choice" that it works just fine. I rewrote the function using a separate variable and this time it works perfectly: Code: function Get_Radio_Selection(options) { var choice = ""; var index; for (var i = 0; i < options.length; i++) { if (options[i].checked == true) { index = i; choice = options[index].value; } } return choice; } Notice the indexing variables in the "if" statement and the assignment to "choice" are different. Does anyone have any suggestions on why this happens? Thanks. I am ignorant in java scripting. I want to use the following java script to create the radiobutton in obiee reporting. This java script will change the regular prompt to radio button,however I do not know how to debug if it is working. Waiting for kind reply. Anil PHP Code: <script type="text/javascript"> function getElementsByClass(searchClass,node,tag) { var classElements = new Array(); if ( node == null ) node = document; if ( tag == null ) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); for (i = 0, j = 0; i < elsLen; i++) { if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; } } return classElements; } var tables = getElementsByClass('DashboardPromptViewTable',null, 'table'); for (var table = 0; table < tables.length; table++){ var stringFunc = ''; var selects = tables[table].getElementsByTagName('select'); if ( selects.length == undefined || selects.length == 0 || selects[0].name === 'Match'){ if (debug === 1) { document.write("No selects found. Continue to next record."+"<BR>");} continue; // This DB Prompt doesn't have any select statements } var spans = getElementsByClass('minibuttonOn',tables[table],'span'); spans[0].style.display="none"; stringFunc = String(spans[0].getElementsByTagName('a')[0].onclick); stringFunc = stringFunc.substr(stringFunc.indexOf('{')+1, stringFunc.length - 2 - stringFunc.indexOf('{')); for (var s =0; s < selects.length; s++){ var new_form = document.createElement('form'); var options = selects[s].getElementsByTagName('option'); new_form.name = selects[s].name+"_radio"; new_form.id = selects[s].id+"_radio"; for (var o=0; o<options.length; o++) { var new_input = document.createElement('input'); new_input.type = 'radio'; new_input.value = options[o].value; new_input.name = selects[s].name+"_radio"; if(selects[s].selectedIndex == o){ new_input.checked = true; } var onClickStringFunc = "var sel = document.getElementById(this.name.substr(0,this.name.length-6));" + "if ( sel == null){"+ "return;"+ "}"+ "var opts = sel.getElementsByTagName('option');"+ "for( var opt = 0; opt < opts.length; opt++){"+ "if( opts[opt].value == this.value){"+ "sel.selectedIndex = opt;"+ "}"+ "}"; new_input.onclick = new Function(onClickStringFunc + stringFunc); var new_text = document.createTextNode(options[o].innerHTML); new_form.appendChild(new_input); new_form.appendChild(new_text); } // end options for loop selects[s].parentNode.insertBefore(new_form,selects[s]); selects[s].style.display="none"; } // end selects for loop } // end table foor loop </script> Hi hope someone here can help me with a problem i'm stuck on. Basically I have 3 radio buttons and one of them has to be selected otherwise display a error message but I cant figure it out. Here is my failed attempt: Code: <html> <head> <link rel="stylesheet" type="text/css" href="css/style.css" /> <script type="text/javascript"> function validateForm() { var x=document.forms["form1"]["name"].value; var y=document.form1.difficulty.value; if (x==null || x=="") { alert("First name must be filled out"); return false; } if (y==false) { alert(y); return false; } } </script> </head> <body> <div id="horizon"> <div id="content"> <img src="images/logo.jpg" width="649" height="410"></div> <div id="content2"><form name ="form1" method ="post" action ="game.php" onSubmit="return validateForm()" > Name: <input type ='text' name='name' /> <br /> <Input type = 'Radio' Name ='difficulty' value= '1' />Easy <Input type = 'Radio' Name ='difficulty' value= '2' />Medium <Input type = 'Radio' Name ='difficulty' value= '3' />Hard <br /><Input type = "Submit" Name = "Submit" VALUE = "Submit"> </form></div></div> </body> </html> Thanks in advance Hi guys...I am new to javascript.....I need help with my code below.I need to change it to " the font size of a paragraph should only be changed if the "Magnified" button is selected AND the mouse cursor is over a paragraph. When the mouse moves over a paragraph, increase the font size by a factor of 3. When it is no longer over the paragraph, reduce the font back to its original size" HELP and thanks.... <html> <head> <title>Assignment 4</title> </head> <body> <p> <script launguage="javascript"> var fontSize = 1; function zoomIn() { fontSize += 0.1; document.body.style.fontSize = fontSize + "em"; } function zoomOut() { fontSize -= 0.1; document.body.style.fontSize = fontSize + "em"; } </script> </p> <h2>Joseph’s text zoom and radio buttons</h2> <p>Click on below buttons and see the text getting zoomed in and out..!!</p> <input type="radio" name="radio" value="fontSize -" onclick = "zoomOut()"/> <label for="original">Standard</label> <br><br> <input type="radio" name="radio" value="fontSize -" onclick = "zoomIn()"/> <label for="zoomed">Magnified</label> </body> </html> As the code example below shows, I have three radio buttons that perform an javascript action when selected. Code: <script language="javascript"> function OnProductClick(product) { if (product.value == "Car") { // do something } else if (product.value == "Boat") { // do something else.. } else if (product.value == "Train") { // do else } } </script> <p>Select option <input type="radio" name="product" onclick="OnProductClick(this)" value="Car">Car</input> <input type="radio" name="product" onclick="OnProductClick(this)" value="Boat"> Boat</input> <input type="radio" name="product" onclick="OnProductClick(this)" value="Train">Train</input> </p> This setup is triggerred by a click, clicking multiple times on the same radio button will yield multiple calls to the OnProductClick function. This last part is what I would like to prevent. Say a user selects the option 'car' and then for some reason clicks on the same option again, this will lead to the OnProductClick function being executed twice. How can I prevent the second execution? Hi, I am trying to use the radio button to display the information for the users. For example, when I click radio button "1", the form should display 1 text box each for first name, last name and so on. When the user clicks on radio button 2, there should be text boxes displayed for user 1 and user 2 to enter their info(first name, last name and so on). I looked at the forum, but couldn't find something similiar.Please advise. Something like ================================ Number of Users 1 2 3 # if radio button 2 is selected then User 1 First Name Last Name User 2 First Name Last Name ================================ The code is as below: <html> <head> <script type="text/javascript"> function showUser() { for(i=0;i<document.form.users.length;i++) { if (document.form.users[i].checked) val=parseInt(document.form.users[i].value); for(j=0;j<=val;j++) { document.form.write("<table cellSpacing=1 cellPadding=0 width=600 border=0>"); document.form.write("<tr> <td width='20%' align='right'><font color="#660000"><b>First Name: </b></font></td>"); document.form.write("<td><input type='text' name='fname1' size='25' maxlength='25'></td></tr>"); document.form.write("<tr><td width='20%' align='right'><font color="#660000"><b>Last Name: </b></font></td>"); document.form.write("<td><input type='text' name='lname1' size='25'maxlength='25'></td></tr>"); document.form.write("</table>"); } } } </script> </head> <form> Check the number of users to display additional input fields.<br><br> <input type="radio" name="users" value="1" onclick = "showUser()"/> 1<br/> <input type="radio" name="users" value="2" onclick = "showUser()"/>2 <br/> <input type="radio" name="users" value="3" onclick = "showUser()"/>3 <br/> </form> </html> ======================================================== Thanks in advance. I have the following code which I want to make a selection and the value should reflect in the text box and the text box should take on that value for future calculations. Code: <!-- Row 13, Col 1 Order Value --> <tr><td colspan="2" align="right">Delivery Options: <input type="radio" name="sapo" value="35" onclick="deliveryCost('35')" /> R35 - SA Post Office <input type="radio" name="sapo" value="80" onclick="deliveryCost('80')" /> R80 - Speed Services <input type="radio" name="sapo" value="150" onclick="deliveryCost('150')" /> R150 - Courier Services </td> <!-- Row 13, Col 2 Order Value Box--> <td colspan="1" align="left"><input style="margin-left: 60px" type="text" name="delivery" size="10" readonly="readonly" /> </td></tr> How can I style the background of a radio button? I have a form where it validates the users has selected one of the radio group values. If they don't the group gets focus and I would like to highlight the radio group. I tried the below it it seems focus goes to the correct group but no background changes. Now I know you can change the background of radios and apply a border of you would like. What am I doing wrong woth this. Code: <script type="text/javascript"> function validate(){ document.forms[0].myradio[0].checked = true document.forms[0].myradio[0].style.border='2px dotted #00f'; document.forms[0].myradio[0].style.backgroundColor='#ffff99'; } </script> <form name="test" name="theForm" onsubmit="validate()"> <input type="radio" name="myradio" /> <input type="radio" name="myradio" /> <input type="radio" name="myradio" /> <BR> <BR> <BR> <BR> <input type="radio" name="myradio2" /> <input type="radio" name="myradio2" /> <input type="radio" name="myradio2" /> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <input type="radio" name="myradio3" /> <input type="radio" name="myradio3" /> <input type="radio" name="myradio3" /> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <input type="radio" name="myradio4" /> <input type="radio" name="myradio4" /> <input type="radio" name="myradio4" /> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <input type="radio" name="myradio5" /> <input type="radio" name="myradio5" /> <input type="radio" name="myradio5" /> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <input type="radio" name="myradio6" /> <input type="radio" name="myradio6" /> <input type="radio" name="myradio6" /> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <input type="submit" value="submit" name="submit"> </form> I'm trying to make radiobuttons with a javascript function that gets called when a button is selected to make an image appear depending on what they click. However, after looking it up for a while I have nothing since some tell me to do document.formName.name.value; some tell me to use an id with my buttons and just use a document.getElementByID(""); etc. This is for a project and we havent used parameters yet and I dont know how to implement them in the HTML as well This is my code: function classchoice() { if(document.getElementById('mage') == true) { classtype = "<img class='display' src='mage.jpg'>"; document.getElementById("picture").innerHTML=classtype; } if(document.classes.classes.paladin == true) { classtype = "<img class='display' src='paladin.jpg'>"; document.getElementById("picture").innerHTML=classtype; } if(document.classes.classes.hunter == true) { classtype = "<img class='display' src='hunter.jpg'>"; document.getElementById("picture").innerHTML=classtype; } if(document.classes.classes.rogue == true) { classtype = "<img class='display' src='rogue.jpg'>"; document.getElementById("picture").innerHTML=classtype; } } This is the HTML: <form name="classes"> <input type="radio" name = "classes" id="mage" onmousedown="classChoice();"> <img src = "mage.jpg" /> <input type="radio" name = "classes" id = "Paladin" onmousedown="classChoice();"> <img src = "Paladin.png" /> <input type="radio" name = "classes" id = "hunter" onmousedown="classChoice();"> <img src = "hunter.PNG" /> <input type="radio" name = "classes" id = "rogue" onmousedown="classChoice();"> <img src = "rogue.jpg" /> </form> I hope it's readable because I copy and pasted my code and the indentations are a bit messed up I'm trying to get the value of a radio button when clicked. I'm dynamically creating these radio buttons while populating a table like this: Code: var rad for(blah blah blah){ insert array data into table... insert array data into table... insert array data into table... rad = document.createElement('input'); rad.setAttribute('type', 'radio'); rad.setAttribute('name', 'options'); rad.setAttribute('value', id); rad.onclick = function() { doSomething() }; cell.appendChild(rad); } If I'm in the doSomething function, how do I access the value or ID of the radio button? Thanks! how would i check to see if my radio button is not checked. I know in javascript the term "checked" works. Is there a way to us unchecked? here is what i am trying to figure out. Code: with (thisform) { if(document.getElementById('m21').unchecked) { window.alert('Please Verify Your Age') m21.focus(); return false;} } Hello, I have a form which can be saved (using a database) and later edited. When restoring data to the form , text fields are easy. There is a simple (existing) JavaScript which restores an option that was selected from a pull-down (select one) menu: Code: for (var q=0;q<document.Simple.Color.options.length;q++) { if (document.Simple.Color.options[q].value == document.Restored.Color.value) { document.Simple.Color.options[q]. selected = true; break; } I'm trying to make it work to restore a value to a radio button as well... but no luck: Code: for (var q=0;q<document.Simple.Color.options.length;q++) { if (document.Simple.Color.options[q].value == document.Restored.Color.value) { document.Simple.Color.options[q]. checked = true; break; } Any ideas? Thanks in advance. ~Wayne |