JavaScript - Please Help! Trouble W/ Radio Button Code..
Hi all. I'm having some trouble w/ my radiobutton code and i dont know why. for instance if i select yes for the 1st radio button, and no for the 2nd, instead of doing what it should do for no, it gets rid of w/e it did for yes.. also, if i enter something into the 1st box, and select another radio button, it deletes w/e is in all boxes...
Code: <html> <head><title>So you want to make a website?</title></head> <body> <script> var lvl=0; var cssbgc=0; var ischecked=0; var isgroup1=0; var isgroup2=0; function get_funcs(lvl){ if(lvl == "Easy"){ a='<hr>\n'; a+='<h1>Website Controls</h1>\n'; a+='Do you want to add a title to your website?<br>\n'; a+='Yes <input onclick="checkchecks()" type="radio" name="group1" value="Yes"> No<input onclick="checkchecks()" type="radio" name="group1" value="No"><br><br>\n'; a+='<div id="websitep">- Does not want website title</div><br>\n'; a+='Do you want to add a background image to your website?<br>\n'; a+='Yes <input onclick="checkchecks()" type="radio" name="group2" value="Yes1"> No<input onclick="checkchecks()" type="radio" name="group2" value="No1"><br><br>\n'; a+='<div id="websiteq">- Does not want website background</div><br>\n'; a+='Your Website scripts:<br><input name="websitescripts" type="text" size="30"> <a href="javascript:answerq(\'websitescripts\');">Help?</a><br>\n'; a+='<br>Your website content (this will appear inbetween <body></body>)<br>This can be done for you if you wish: <a href="javascript:answerq(\'webcontent\');">Help?</a><br><textarea rows="15" cols="45" id="websitecontent" name="websitecontent"></textarea><br>\n'; a+='<input type="button" value="Submit" name="sub1" id="sub1" onClick="checkconts(lvl)"><br>\n'; a+='<h1>Your Website</h1><hr>\n'; a+='<textarea rows="15" cols="45" id="websitelvl" name="websitelvl"></textarea>\n'; document.getElementById("websitecontrols").innerHTML = a; }else if(lvl == "Medium"){ a='<hr>\n'; a+='<h1>Website Controls</h1>\n'; a+='<table>\n'; a+='<tr><td>\n'; a+='Your Website background picture (if any):<br><input name="webback" type="file" size="30"> <a href="javascript:answerq(\'webback\');">Help?</a><br>\n'; a+='Your Website scripts:<br><input name="websitescripts" type="text" size="30"> <a href="javascript:answerq(\'websitescripts\');">Help?</a><br>\n'; a+='Do you want a website background color using css?<br><br>Yes<input onclick="check()" type="checkbox" value="Yes" id="group1" name="group1"> No<input onclick="check()" type="checkbox" value="No" id="group1" name="group1"><br>\n<div id="cssbc"></div>\n'; a+='<br>Your website content (this will appear inbetween <body></body>)<br>This can be done for you if you wish: <a href="javascript:answerq(\'webcontent\');">Help?</a><br><textarea rows="15" cols="45" id="websitecontent" name="websitecontent"></textarea><br>\n'; a+='<input type="button" value="Submit" name="sub1" id="sub1" onClick="checkconts(lvl)"><br>\n'; a+='</td></tr>\n'; a+='</table>\n'; a+='<h1>Your Website</h1><hr>\n'; a+='<textarea rows="15" cols="45" id="websitelvl" name="websitelvl"></textarea>\n'; document.getElementById("websitecontrols").innerHTML = a; } } function checkchecks(){ for (i=0; i<document.forms.radioform.group1.length; i++){ if(i == 0 && document.forms.radioform.group1[i].checked == true){ ischecked = 1; isgroup1 = 1; isgroup2 = 0; }else if(i == 1 && document.forms.radioform.group1[i].checked == true){ ischecked = 0; isgroup1 = 0; isgroup2 = 0; } } for (i=0; i<document.forms.radioform.group2.length; i++){ if(i == 0 && document.forms.radioform.group2[i].checked == true){ ischecked = 1; isgroup1 = 0; isgroup2 = 1; }else if(i == 1 && document.forms.radioform.group2[i].checked == true){ ischecked = 0; isgroup1 = 0; isgroup2 = 0; } } if(isgroup1 == 1){ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); a='Your Website Title:<br><input name="webtitle" type="text" size="30"> <a href="javascript:answerq(\'webtitle\');">Help?</a><br>\n'; document.getElementById("websitep").innerHTML = a; }else{ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); a='- Does not want website title<br>\n'; document.getElementById("websitep").innerHTML = a; } if(isgroup2 == 1){ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); b='Your Website background picture (if any):<br><input name="webback" type="file" size="30"> <a href="javascript:answerq(\'webback\');">Help?</a><br>\n'; document.getElementById("websiteq").innerHTML = b; }else{ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); a='- Does not want website background image<br>\n'; document.getElementById("websiteq").innerHTML = b; } alert('isgroup1:'+isgroup1+'|'+'isgroup2:'+isgroup2+''); } function check(){ for (i=0; i<document.forms.radioform.website.length; i++){ if(document.forms.radioform.website[i].checked == true){ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); if(i == 0 && document.forms.radioform.website[1].checked == true){ cssbgc = 0; document.forms.radioform.website[1].checked = false; }else if(i == 1 && document.forms.radioform.website[0].checked == true){ cssbgc = 1; document.forms.radioform.website[0].checked = false; } } } if(cssbgc == 0){ alert('ok then'); }else if(cssbgc == 1){ document.getElementById("cssbc").innerHTML = ""; } } function preview_website(){ if(document.forms.radioform.websitecontent.value != ""){ alert('Previewing your website...'); a='<center><h1>Website Preview<\/h1><\/center>\n<br><br>\n'; a+=document.forms.radioform.websitecontent.value+'\n'; document.write(a); } } function previewconts(){ if(document.forms.radioform.websitecontent.value != "" || document.forms.radioform.webtitle.value != "" || document.forms.radioform.webback.value != "" || document.forms.radioform.weblvl.value != ""){ preview_website(lvl); }else{ alert('Please enter something into the box before continuing...'); } } function checkconts(lvl){ if(document.forms.radioform.websitecontent == null || document.forms.radioform.websitecontent == null || document.forms.radioform.webtitle == null || document.forms.radioform.webback == null || document.forms.radioform.weblvl == null){ alert('Please select an option 1st...'); }else if(document.forms.radioform.websitecontent.value == null || document.forms.radioform.websitecontent.value == null || document.forms.radioform.webtitle.value == null || document.forms.radioform.webback.value == null || document.forms.radioform.weblvl.value == null){ alert('Please enter something into the box before continuing...'); }else{ a=document.forms.radioform.websitelvl.value = website(lvl); document.getElementById('websitecontent').innerHTML = a; } } function get_option(which){ if(which == "webtitle"){ return document.radioform.webtitle.value; }else if(which == "webback"){ return document.radioform.webback.value; }else if(which == "websitecontent"){ return document.radioform.websitecontent.value; }else if(which == "websitescripts"){ return document.radioform.websitescripts.value; } } function answerq(divname){ if(divname == "webtitle"){ alert('This is where you enter the name of your website that appears in the top left corner of your web browser.'); }else if(divname == "webback"){ alert('This is where you enter a background image link or a select a picture from your hard drive (IE: When selected you will see something like C:/mypics/pic.jpg appear in the box).'); }else if(divname == "webcontent"){ alert('this is where you enter what you want to appear on the page (this can be done for you if you wish).'); }else if(divname == "websitescripts"){ alert('This is where you enter any file locations to your script file(s).'); } } function website(lvl){ if(lvl == "Easy"){ a = "<html>\n"; a += "<head><title>"+get_option('webtitle')+"</title></head>\n"; if(document.forms.radioform.webback.value == ""){ a += "<body>\n"; }else{ a += "<body background=\""+get_option('webback')+"\">\n"; } if(document.forms.radioform.websitecontent.value != ""){ a += get_option('websitecontent')+"\n"; a += "</body>\n"; }else{ a += "</body>\n"; } a += "</html>\n"; } return a; } function callcontrols(lvl){ if(lvl == "Easy" || lvl == "Medium" || lvl=="Hard" || lvl == "Extreme"){ get_funcs(lvl); }else{ document.getElementById("websitecontrols").innerHTML = "NO!"; } } function getwebsite(lvl){ if(lvl == "Easy" || lvl == "Medium" || lvl == "Hard" || lvl == "Extreme"){ callcontrols(lvl); } } function get_radio_value() { for (var i=0; i < document.radioform.website.length; i++) { if (document.radioform.website[i].checked) { var rad_val = document.radioform.website[i].value; getwebsite(rad_val); } } lvl = rad_val; alert(lvl); } </script> <center> <h1>So you want to build a website?</h1><br> Easy - HTML Website<br> Medium - HTML / JavaScript / CSS Website<br> Hard - HTML / JavaScript / CSS / PHP Website<br> Extreme - HTML / Encrypted HTML / JavaScript / Encrypted JavaScript / CSS / PHP Website<br><br> You have four choices: <br> <form name="radioform"> Step 1:<br>Choose an option<br><br> Easy<input type="radio" onclick="get_radio_value()" name="website" value="Easy">Medium<input onclick="get_radio_value()" type="radio" name="website" value="Medium">Hard<input onclick="get_radio_value()" type="radio" name="website" value="Hard">Extreme<input onclick="get_radio_value()" type="radio" name="website" value="Extreme"><br><br> <div id="websitecontrols"></div> </form> </center> </body> </html> The functions you are looking at are checkchecks() and checkconts(). here's the functions: checkchecks(): Code: function checkchecks(){ for (i=0; i<document.forms.radioform.group1.length; i++){ if(i == 0 && document.forms.radioform.group1[i].checked == true){ ischecked = 1; isgroup1 = 1; isgroup2 = 0; }else if(i == 1 && document.forms.radioform.group1[i].checked == true){ ischecked = 0; isgroup1 = 0; isgroup2 = 0; } } for (i=0; i<document.forms.radioform.group2.length; i++){ if(i == 0 && document.forms.radioform.group2[i].checked == true){ ischecked = 1; isgroup1 = 0; isgroup2 = 1; }else if(i == 1 && document.forms.radioform.group2[i].checked == true){ ischecked = 0; isgroup1 = 0; isgroup2 = 0; } } if(isgroup1 == 1){ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); a='Your Website Title:<br><input name="webtitle" type="text" size="30"> <a href="javascript:answerq(\'webtitle\');">Help?</a><br>\n'; document.getElementById("websitep").innerHTML = a; }else{ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); a='- Does not want website title<br>\n'; document.getElementById("websitep").innerHTML = a; } if(isgroup2 == 1){ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); b='Your Website background picture (if any):<br><input name="webback" type="file" size="30"> <a href="javascript:answerq(\'webback\');">Help?</a><br>\n'; document.getElementById("websiteq").innerHTML = b; }else{ alert('Make SURE this option is what you want. If you select another option after entering data, it will erase your data...'); a='- Does not want website background image<br>\n'; document.getElementById("websiteq").innerHTML = b; } alert('isgroup1:'+isgroup1+'|'+'isgroup2:'+isgroup2+''); } checkconts(): Code: function checkconts(lvl){ if(document.forms.radioform.websitecontent == null || document.forms.radioform.websitecontent == null || document.forms.radioform.webtitle == null || document.forms.radioform.webback == null || document.forms.radioform.weblvl == null){ alert('Please select an option 1st...'); }else if(document.forms.radioform.websitecontent.value == null || document.forms.radioform.websitecontent.value == null || document.forms.radioform.webtitle.value == null || document.forms.radioform.webback.value == null || document.forms.radioform.weblvl.value == null){ alert('Please enter something into the box before continuing...'); }else{ a=document.forms.radioform.websitelvl.value = website(lvl); document.getElementById('websitecontent').innerHTML = a; } } Similar TutorialsHere's my HTML: Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>The Happy Hoppin' Hotel</title> <script src="happyhoppin.js" language="javascript" type="text/javascript"></script> </head> <body> <h1>The Happy Hoppin' Hotel Checkout Page</h1> <h2>Fill out the form below to calculate balance due</h2> <form> Guest ID Number: <input type="text" id="guestID" /> <br /> <br /> Room Type: <select id="roomType"> <option></option> <option>Parlor</option> <option>Single</option> <option>Double</option> </select> <br /> <br /> Length of Stay: <input type="text" id="stayLength" /> <br /> <br /> Number of Drinks: <input type="text" id="drinkNumber" /> <br /> <br /> Number of Towels: <input type="text" id="towelNumber" /> <br /> <br /> Number of Flushes: <input type="text" id="flushNumber" /> <br /> <br /> Bug Complaints?: <br /> <form name="bugComplaintRadio"> <input type="radio" name="bugComplaint" value="No" />No</label> <br /> <input type="radio" name="bugComplaint" value="Yes" />Yes</label> <br /> </form> <br /> Customer Comments: <br /> <textarea name="customerComment" cols="50" rows="5">Enter your comments here...</textarea> <br /> <br /> <input type="button" onclick="calculateBill()" value="Calculate Bill"> </form> </body> </html> Here's my Javascript: Code: const parlorPrice = 80; const singlePrice = 100; const doublePrice = 150; const drinkPrice = 5; const towelPrice = 3; const flushPrice = 1; var guestID = 0; var roomPrice = 0; var stayLength = 0; var drinkNumber = 0; var towelNumber = 0; var flushNumber = 0; var totalDue = 0; var totalCharge = 0; function calculateBill(){ validateForm(); //roomType// if(roomType == "Parlor"){ roomPrice = parlorPrice; } if(roomType == "Single"){ roomPrice = singlePrice; } if(roomType == "Double"){ roomPrice = doublePrice; } //roomType// //drinkCharge// drinkCharge = drinkNumber * drinkPrice; //drinkCharge// //towelCharge// towelCharge = towelNumber * towelPrice; //towelCharge// //flushCharge// flushCharge = flushNumber * flushPrice; //flushCharge// //totalCharge// totalCharge = roomPrice + drinkCharge + towelCharge + flushCharge; //totalCharge// //**bugDiscount**// function getCheckedRadio() { bugValue = ""; bugLength = document.bugComplaintRadio.bugComplaint.length; var bugDiscount = 0; for (x = 0; x < bugLength; x ++) { if (document.bugComplaintRadio.bugComplaint[x].checked) { bugValue = document.bugComplaintRadio.bugComplaint[x].value; } } if (bugValue == "") { alert("You did not choose whether you had a bug complaint or not"); } if (bugValue = "No"){ bugDiscount = 0; } if (bugValue = "Yes"){ bugDiscount = 20; } } //**bugDiscount**// getCheckedRadio(); //totalDue// totalDue = totalCharge + bugDiscount //totalDue// displayBill(); } function validateForm(){ //guestID// guestID = parseInt(document.getElementById("guestID").value); if(isNaN(guestID)){ alert("Guest ID must be a number"); return; } if(guestID <= 0){ alert("Guest ID must be greater than zero"); return; } //guestID// //roomType// roomType = document.getElementById("roomType").value; if(roomType == ""){ alert("Room type must be selected"); return; } //roomType// //stayLength// stayLength = parseInt(document.getElementById("stayLength").value); if(isNaN(stayLength)){ alert("Length of stay must be a number"); return; } if(stayLength <= 0){ alert("Length of stay must be greater than zero"); return; } //stayLength// //drinkNumber// drinkNumber = parseInt(document.getElementById("drinkNumber").value); if(isNaN(drinkNumber)){ alert("Number of drinks must be a number"); return; } if(drinkNumber <= 0){ alert("Number of drinks must be greater than zero"); return; } if(drinkNumber > 25){ alert("Number of drinks has exceeded 25"); return; } //drinkNumber// //towelNumber// towelNumber = parseInt(document.getElementById("towelNumber").value); if(isNaN(towelNumber)){ alert("Number of towels must be a number"); return; } if(towelNumber <= 0){ alert("Number of towels must be greater than zero"); return; } //towelNumber// //flushNumber// flushNumber = parseInt(document.getElementById("flushNumber").value); if(isNaN(flushNumber)){ alert("Number of flushes must be a number"); return; } if(flushNumber <= 0){ alert("Number of flushes must be greater than zero"); return; } //flushNumber// //customerComment// customerComment = document.getElementById("customerComment"); //customerComment// } function displayBill(){ var newPage = "<html><head><title>Billing Summary</title></head>"; newPage += "<body><h1>Happy Hoppin Hotel</h1>"; newPage += "<h2>Guest Billing Statement</h2>"; newPage += "Guest Identification: #" + guestID; newPage += "<br />"; newPage += "Room Type: " + roomType; newPage += "<br />"; newPage += "Room Charge: $" + roomPrice; newPage += "<br />"; newPage += "Length of Stay: " + stayLength + " days"; newPage += "<br />"; newPage += "Drink Charge: $" + drinkCharge; newPage += "<br />"; newPage += "Towel Charge: $" + towelCharge; newPage += "<br />"; newPage += "Flushing Charge: $" + flushCharge; newPage += "<br />"; newPage += "Total Charge: $" + totalCharge; newPage += "<br />"; newPage += "Discount: $" + bugDiscount; newPage += "<br />"; newPage += "Total Due: $" + totalDue; newPage += "<br />"; newPage += "<h3>Come back and visit us again at the Happy Hoppin' Hotel!</h3>"; var z = window.open("","","width=400,height=500"); z.document.write(newPage); z.document.close(); } My question is, I've been spending countless hours trying to: 1. Make two radio buttons indicating "No" and "Yes", 2. Retrieve which selection the user has made, 3. Change the value of "bugDiscount" or the amount of money ($20 or $0) depending on which choice the user made; $0 for No & $20 for Yes. 4. Subtract the value of bugDiscount (0 or 20) from the totalCharge to get TotalDue I know I'm close, but I've tried any number of variations in my code and I still can't seem to get it right. Can anyone help? I 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 here is the html code that i have PHP Code: <td valign="middle" valign="middle"> <input type="radio" name="gender" id="genderM" value="Male" /> Male <input type="radio" name="gender" id="genderFM" value="Female" /> Female </td> and here is the js funtion PHP Code: var $j = jQuery.noConflict(); function isValidEmail(str) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); } function validateForm(){ var firstName; var lastName; var email; var mobile; var comment; var error; firstName = $j('#firstName').val(); lastName = $j('#lastName').val(); email = $j('#email').val(); mobile = $j('#mobile').val(); comment = $j('#comment').val(); if(firstName=='' || firstName.length < 3){ error = 'Please Enter Your First Name'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } if(lastName=='' || lastName.length < 3){ error = 'Please Enter Your Second Name'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } //mob //$jmob_pattern = '^\d{10}$j'; if(mobile.length != 10 || isNaN(mobile)){ error = 'Please Enter Your Mobile Number'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } if(email=='' || !isValidEmail(email)){ error = 'Please Enter Your Email Address'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } if(comment.length < 5){ error = 'Please Enter A Comment'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } return true; } Does anybody know how i check to see if the radio button is select and also can anybody tell me how i can check for an email in the correct format the function isValidEmail in the above alows emails to pass through if they are in this format aaa@aaa. i only want them to go through if they are aaa@aaa.com Thanks for your help if you give it Hi All, I am very new to HTML programming and Javascripts. What I am trying to accomplish is I have a radio button "RequiredApprovalYesNo". When the selection is "Yes", I need fields "Approver" and "ApproverEmail" to be required upon submit. I also need to make sure that a selection is made with this radio button of either Yes or No. Here is my HTML code for these three fields and was wondering if someone could show me how to code this script or, give me an example of a Radio button selection resulting in additional fields being required or not. Thank you in advance. Code: <td> <input type="radio" name="RequireApprovalYesNo" id="RequireApprovalYesNo" value="Yes">Yes <input type="radio" name="RequireApprovalYesNo" id="RequireApprovalYesNo" value="No">No </tr> <tr> <td><table width="190" border="0" align="right" cellspacing="0"> <tr> </tr> </table></td> <tr> <td colspan="3"><div align="left"><em><strong><b style= 'color: red;'>If Yes, You Must Enter an Approver with a corresponding Email:</b> </strong></em></div></td> </tr> <tr> <td height="25"><div align="right">Approver:</div></td> <td> </td> <td><input name="Approver" type="text" id="Approver" size="40";"></td> </tr> <tr> <td height="25"><div align="right"> <p>Approver's Email: </p> </div></td> <td> </td> <td><input name="ApprovalEmail" type="text" id="ApprovalEmail" size="40"></td> </tr> <tr> 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 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. 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> Hi all, This code works to place a mark in a radio button (check one) in a "restored" form: Code: for (var q=0;q<document.Simple.Colors.length;q++) { if (document.Simple.Colors[q].value == document.Restored.Colors.value) { document.Simple.Colors[q].checked = true ; break; } I'm curious why it won't work for a check box (check more than one): I figured it would be the same... no? Thanks, ~Wayne I am looking for some help similar to this old post: http://www.codingforums.com/showthread.php?t=19588 I want my radio buttons to become submit buttons as well. So when a user clicks on a radio button it submits the action and refreshes the page accordingly. As of now using only using onclick="this.form.submit()" the page only refreshes with no change. Here is a copy of the entire form. It is a custom shipping options form (I did not create it). Any help would be appreciated. Code: <!-- Custom shipping form --> <form action="" method="post" name="update_shipping"> <table width="300"> <tr> <td><label> <input <?php if (!(strcmp($_SESSION['shippingmethod'],"1"))) {echo "checked=\"checked\"";} ?> name="grpshipping" type="radio" id="grpshipping_0" value="1" checked="checked" onclick="this.form.submit()"/> Ground Shipping</label></td> <td class="style261"><?php echo date("m/d/Y",$grounddelivery);?></td> <td class="style261">$<?php if(($_SESSION['roundedweight']) >= 21){ echo number_format($row_rsshippingprices['Ground'] + ($row_rsshippingpricesover21['Ground'] * ($varWeightover)) ,2); } else { echo number_format($row_rsshippingprices['Ground'],2); } ?></td> </tr> <?php if($rainbowcart->ConditionalTotal("Quantity", "groundshiponly", "1") == 0) {//show other shipping options ONLY if a ground shipping only item is not in the cart ?> <tr> <td><label> <input <?php if (!(strcmp($_SESSION['shippingmethod'],"2"))) {echo "checked=\"checked\"";} ?> type="radio" name="grpshipping" value="2" id="grpshipping_1" onclick="this.form.submit()" /> Fedex 3 Day</label></td> <td class="style261"><?php echo date("m/d/Y",$fedex3delivery);?></td> <td class="style261">$<?php if(($_SESSION['roundedweight']) >= 21){ echo number_format($row_rsshippingprices['Fedex3'] + ($row_rsshippingpricesover21['Fedex3'] * ($varWeightover)) ,2); } else { echo number_format($row_rsshippingprices['Fedex3'],2); } ?></td> </tr> <tr> <td><label> <input <?php if (!(strcmp($_SESSION['shippingmethod'],"3"))) {echo "checked=\"checked\"";} ?> type="radio" name="grpshipping" value="3" id="grpshipping_2" onclick="this.form.submit()"/> Fedex 2 Day</label></td> <td class="style261"><?php echo date("m/d/Y",$fedex2delivery);?></td> <td class="style261">$<?php if(($_SESSION['roundedweight']) >= 21){ echo number_format($row_rsshippingprices['Fedex2'] + ($row_rsshippingpricesover21['Fedex2'] * ($varWeightover)) ,2); } else { echo number_format($row_rsshippingprices['Fedex2'],2); } ?></td> </tr> <tr> <td><label> <input <?php if (!(strcmp($_SESSION['shippingmethod'],"4"))) {echo "checked=\"checked\"";} ?> type="radio" name="grpshipping" value="4" id="grpshipping_3" onclick="this.form.submit()"/> Next Day PM</label></td> <td class="style261"><?php echo date("m/d/Y",$nddelivery);?></td> <td class="style261">$<?php if(($_SESSION['roundedweight']) >= 21){ echo number_format($row_rsshippingprices['ONPM'] + ($row_rsshippingpricesover21['ONPM'] * ($varWeightover)) ,2); } else { echo number_format($row_rsshippingprices['ONPM'],2); } ?></td> </tr> <tr> <td><label> <input <?php if (!(strcmp($_SESSION['shippingmethod'],"5"))) {echo "checked=\"checked\"";} ?> type="radio" name="grpshipping" value="5" id="grpshipping_4" onclick="this.form.submit()"/> Next Day AM</label></td> <td class="style261"><?php echo date("m/d/Y",$nddelivery);?></td> <td class="style261">$<?php if(($_SESSION['roundedweight']) >= 21){ echo number_format($row_rsshippingprices['ONAM'] + ($row_rsshippingpricesover21['ONAM'] * ($varWeightover)) ,2); } else { echo number_format($row_rsshippingprices['ONAM'],2); } ?></td> </tr> </table> <table class="eC_ButtonWrapper" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="left"><center>Choose an option above and click update to change your shipping preference.<br /><b>PLEASE BE SURE AND PRESS UPDATE IF YOU CHANGE YOUR SHIPPING OPTION<br /><br /><input type="image" name="Update_shipping" id="Update_shipping" value="Update_shipping" alt="Update_shipping" src="WA_eCart/Images/Nautica/Btn4_EN_update.gif" class="eC_ImageButton" /></center></td> </tr> </table> <?php } //end of show if ground ship item is not in the cart?> <?php if($rainbowcart->ConditionalTotal("Quantity", "groundshiponly", "1") <> 0) { ?> <font color="red">The cart contains a Ground Shipping only item, this order can only be shipped ground</font><br /><br /> <?php } ?> </form> <!-- End of Custom shipping form --> 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; } 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> 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 Hello I am very new to Java Script. I am working on a website which requires me to use it. The site is selling items that are priced depending on the size. There are 6 fixed sizes for the width and the length can come in any size 1 through 200. So that will be user input on the length and radio buttons on the width. I am trying to have the user put in the length and click a radio button for width and then calculate the price by multiplying them. I am trying to get 1 price to display right now just to test it but I can't figure out why it isn't working. Again I am fairly new at this so there might be multiple problems with my code. Thanks in advance for the help. Code: <script type = "text/javascript"> function buttoncheck(){ var selection1 = document.menu.selection; if (selection1 == 60) {choice1()} if (selection1 == 50) {choice2()} if (selection1 == 25) {choice3()} if (selection1 == 10) {choice4()} if (selection1 == 4) {choice5()} if (selection1 == 2) {choice6()} function choice1(){ var len = Number(document.getElementById("length").value) || 0; var area1 = selection1 * len; var cost = 0; if (area1 == 60) { cost = 60} return cost; document.getElementById("TotalCost").innerHTML = "$" + cost.toFixed(2); } Code: <h4 class="auto-style3">Pricing Calculator:</h4> <p> <form method="post"> <div class="auto-style1"> <span class="auto-style2">LENGTH:</span> <input name="length" type="text" id="length" /><br /> </form> <div class="auto-style1"> <form name="menu"> <input type="radio" value="60" name="selection">60 inch</input> <input type="radio" value="50" name="selection">50 inch</input> <input type="radio" value="25" name="selection">25 inch</input> <input type="radio" value="10" name="selection">10 inch</input> <input type="radio" value="4" name="selection">4 inch label</input> <input type="radio" value="2" name="selection">2 inch label</input> </form> </div> <p class="auto-style1"> <input type="button" value="Calculate" onClick="buttoncheck()" </p> </p> <p> </p> <span id="TotalCost"></span><script type = "text/javascript"> document.getElementById("TotalCost").innerHTML = "Total cost is $" + cost.toFixed(2); 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 Hello, I am trying to implement a radio button option so that users can search specific sites such as google, yahoo, or bing from input box. Here's my code. Please help. <script type="text/javascript"> <!-- function goSearch(){ //search according to checked radio button } --> </script> <h1>Search Option</h1><br /> <input type="radio" name="search" id="google"/><a href="http://www.google.com" rel="nofollow" target="_blank"><img src="images/google.jpg" /></a> <input type="radio" name="search" id="yahoo"/><a href="http://www.yahoo.com" rel="nofollow" target="_blank"><img src="images/yahoo.jpg" /> </a> <input type="radio" name="search" id="bing" /><a href="http://www.bing.com" rel="nofollow" target="_blank"><img src="images/bing.jpg" /> </a> <br /><br /> <form method="get" action="goSearch()" rel="nofollow" target="_blank"> <div style="border:1px solid black;padding:4px;width:20em;"> <table border="0" cellpadding="0"> <tr> <td> <input type="text" name="q" size="25" maxlength="255" value="" /> <input type="submit" value="Search" /> </td> </tr> </table> </div> </form> 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> hi, how I can execute this operation : for example x=a+b where "a" it can have value 0 or 1 based on the selection of the radio button. when a user select 1 on the radio button x=1+b when user select 0 x=0+b . please help me. 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 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> |