JavaScript - Checkbox Not Working
I am trying to make a checkbox on the screen so that when I check or uncheck it a word will print on the screen. Unfortunately it is not working. The checkbox shows up but nothing happens when I check/uncheck it.
Also, even though that part of the code isn't working everything that comes after it that is suppose to print onto the screen is also not printing. I am assuming it is because there is an error and cannot reach that part of the code. I am used to programming in Java and using eclipse which has a debugger in it and was wondering if there is a nice editor/debugger like eclipse I can use for JavaScript. Thanks. Code: <html> <title>Welcome :D</title> <head> <style type="text/css"> body { background-color:F0F0F0; } </style> </head> <body> CheckBox1 <input type="checkbox" id="tandc" onClick="validate()" /> <script type="text/javascript"> fuction validate(){ if(cb1.onClick){ document.write("Print something"); }else{ document.write("Now print something?"); } } document.write("</br></br></br></br>"); document.write("Hello, thank you for visiting my webpage I hope you like it."); </script> </body> </html> Similar TutorialsHi all, I have a script that checks and unchecks all boxes depending on the current state of the box. I have used it on one page and it works perfectly fine... copied it over to a new page where I wanted to reuse and can't get it to work. Code: <script type="text/javascript"> function check_all(form, input_name, self){ if(!input_name){ return; } var input = form.getElementsByTagName('input'); for(var i = 0; i < input.length; i++){ if(input[i].name.indexOf(input_name)!=-1){ input[i].checked = self.checked; } } } </script> Code: <input type="checkbox" name="notimportant" onclick="check_all(document.form1, 'check', this)" value="test" /><b>All</b> Code: <input type="checkbox" name="check_list[]" value="<?php echo $row2['email'];?>"> Code: <form name="form1" action="emailspecificcandidates.php" method="post"> As I say this script works perfectly on another page, and as far as i can see i have copied everything over correctly. Can anyone spot anything wrong? Many thanks, Greens85 EDIT: I think this may be broken because of the way I presenting it with php on my page... i copied a seperate entity of the script onto the page and it worked first time. Guys Need Help here... here i send attach my script. i need combine 2 function script into . but i can figure it out. can everyone helps me. Code: <?php require_once('../Connections/koneksi.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; switch(newcell.childNodes[0].type) { case "text": newcell.childNodes[0].value = ""; break; case "checkbox": newcell.childNodes[0].checked = false; break; case "select-one": newcell.childNodes[0].selectedIndex = 0; break; } } } function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if(null != chkbox && true == chkbox.checked) { if(rowCount <= 1) { alert("Cant delete all rows"); break; } table.deleteRow(i); rowCount--; i--; } } }catch(e) { alert(e); } } function selectnama() { var i = 0; i = document.form1.txtcustomer.value; i= i.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' '); //alert(cust[i]); document.form1.select2.value = i; } function change2() { document.form1.txtcustomer.value = document.form1.select2.value; } function enable_text(status) { status=!status; document.form1.qty_material.disabled = status; } </script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form name="form1" method="post" > <table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="6"> <tr> <td style="width:20px;"><INPUT type="checkbox" name="chk" /></td> <td> <input type="text" name="txttipe[]" placeholder="Tipe" id="txttipe" size="40"/> / <input name="txtcustomer" type="text" id="txtcustomer" onkeyup="selectnama()" size="10" maxlength="10"/> <select name="select2" style="color:blue;" onchange="change2()" > <option></option> <?php $query = mysql_query("select right(KUNNR,6), NAME1 from kna1 order by NAME1") or die(mysql_error()); $query3 = mysql_query("select * from spg_kna1_exception order by NAME1") or die(mysql_error()); while($row = mysql_fetch_array($query)) { echo "<option value=".$row[0].">$row[1] - $row[0]</option>"; } while($row3 = mysql_fetch_array($query3)) { echo "<option value=".$row3[0].">$row3[1] - $row3[0]</option>"; } ?> </select>; <input type="checkbox" name="qty_matnr" onClick="enable_text(this.checked)" > Qty <input type="text" name="qty_material"> </td> </tr> </table> <INPUT type="button" value="Add Model" onclick="addRow('dataTable')" /> <INPUT type="button" value="Delete Model" onclick="deleteRow('dataTable')" /> </form> </body> </html> So here's what i want to do: i have 2 checkboxes, when Checkbox A is checked, i want to automatically check the checkbox B. When A is unchecked, then uncheck B how can i do that? thanks a lot ! Hi, If I have two check-boxes and one is already checked and then the is checked, how would I get it to uncheck the first one using JavaScript. Thanks, Cs1h Looking for a simple method to Toggle textbox yes/no using CheckBox This example does not work! But I hope this will help explain what I am trying to do Code: <SCRIPT LANGUAGE="JavaScript"> function YesOrNo() { if(document.formname.checkboxname.checked) { document.formname.textname.disabled='Yes'; } else { document.formname.textname.disabled='No'; }} </SCRIPT> <form name="formname"> <input type="text" size="10" name="textname"> <input type="checkbox" onclick="YesOrNo()" name="checkboxname" value="Yes"> </form> Hi, I have a checkbox and when I "check" I want to add a <p> inside of an <div> and when I "uncheck" I want to remove the <p> from <div>. I've tried this but it's not working Code: $('#checkbox').change(function(){ $('div').html('<p>Some Text</p>'); }, function() { $('div').remove('<p>Some Text</p>'); }); I hope you can help.. Regards I am making a google CSE for a client site and I need to concatenate checkbox values together on the end of another form value. So I have a text box and the checkboxes: Code: <input name="q" cid="q" size="25" maxlength="50" value"off road tires"> <input type="checkbox" name="q" id="q" value="cooper|hankook" /> <input type="checkbox" name="q" id="q" value="Mickey Thompson|Bfgoodrich|GBC" /> <input type="checkbox" name="q" id="q" value="Hoosier|Nitto" /> For Google the URL string needs to be like this: Code: results.html?q=off road tires AND inurl:cooper|hankook|Mickey Thompson|Bfgoodrich|GBC this string says the person did not check the last box. I am having a hard time trying to do this. If you guys know a better way to accomplish the same thing I am all ears but I thought I would bring it to the experts and see what they had to say. Thanks for the help! I was trying to get total amount from these all checkbox, when I submit, but I don't know somewhere wrong. Please help me out. Here is my code: <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title> Form </title> <script language = "JavaScript"> // The event handler functions for the check boxes function appleHandler() { var number = document.orderForm.apples.value; total = total + number * 0.59; } function orangeHandler() { var number = document.orderForm.oranges.value; total = total + number * 0.49; } function bananaHandler() { var number = document.orderForm.bananas.value; total = total + number * 0.39; } // The event handler function to produce the total cost message // when "submit" is clicked function finish() { total = total * 1.05; alert("Thank you for your order \n" + "Your total cost is: $" + total + "\n"); } </script> </head> <body> <h3> Order Form </h3> <form name = "orderForm" onSubmit = "finish()"> <p> <input type = "checkbox" name = "apples" size = "3" onClick = "appleHandler()" /> Apples </p><p> <input type = "checkbox" name = "oranges" size = "3" onClick = "orangeHandler()" /> Oranges </p><p> <input type = "checkbox" name = "bananas" size = "3" onClick = "bananaHandler()" /> Bananas </p><p> <input type = "reset" name = "reset" /> <input type = "submit" name = "submit" /> </p> </form> </body> </html> All, I have the following code: Code: var poststr = "statusupdate=" + encodeURI( document.getElementById("statusupdate").value ) + "&user_name=" + encodeURI( document.getElementById("user_name").value ) + "&twitteryes=" + encodeURI ( document.getElementById("twitteryes").checked ) + "¤tstatus=" + encodeURI( document.getElementById("currentstatus").value ); It seems to be failing on the Code: "&twitteryes=" + encodeURI (document.getElementById("twitteryes").checked ) If I remove the part I just showed it works fine. This is the HTML code: Code: <input type="checkbox" name="twitteryes" value="yesplease" /> Thanks in advance. Hi. I have a list of checkboxes with Ids that go something like; chk_AGE=16 chk_AGE=17 chk_AGE=18 chk_GENDER=MALE chk_GENDER=FEMALE chk_RANK=OFFICER chk_RANKE=OTHER When a user checks/unchecks '16' (for example), I want javascript to look up how many checkboxes in my form have an Id that begins with "chk_AGE" and return the number in an alert box ("3" in this case). If my user had checked/unchecked "Officer" an alert box would return "2" as there are 2 checkboxes that begin "chk_RANK". Can someone please help with this? I am a complete javascript beginner. i have a checkbox: Code: <input type="checkbox" id="my_checkbox" name="option2" value="Check" checked>Check i want to add an event to it so that i do something to a dom element when it is checked and when it is not checked.What is the event and how can i get its status? I am using a checkbox to turn on and off some labels on a line on a google map, which is working fine if the line is already drawn, using this function: Code: function distboxclick(labelsbox) { if (labelsbox.checked) { for (var i=0; i<distmarkers.length; i++) { if (distmarkers[i].mLabel != null) distmarkers[i].mLabel.show(); } } else { for (var i=0; i<distmarkers.length; i++) { if (distmarkers[i].mLabel != null) distmarkers[i].mLabel.hide(); } } } but the thing is that I want the labels to be turned off to begin with and switched on if the box is checked. Which is also fine, using this snippet: Code: if (prevMarker){ var dist = getSegmentDistance(prevMarker.mMarker, distmarker); var label = new ELabel(point, dist, "labelstyle", new GSize(10,-15)); distmarkers[numMarkers-1].mLabel = label; distlabels.push(label); map.addOverlay(label); label.hide(); drawOverlay(); } but what I am trying to do now is make sure that if the box is checked before the line starts to be drawn then the labels will come up straight away. So I figured all I had to do was add in the following to the above: Code: if (labelsbox.checked){ label.show(); } else { label.hide(); } and pass the labelsbox variable to the function where that is all happening. But instead I get a "labelsbox is undefined" error... If I take that if statement out I see that labelsbox is defined, but that doesn't really help. I guess I'm putting things in the wrong order or something, but I'm kind of baffled. You can see my test page here. Thanks in advance for any suggestions. Hi everyone; I have created a JavaScript function to show either a enabled or a disabled text input field depending on whether a checkbox has been clicked or not. It works great for that purpose. However, when I use it for more than one text input field and I submit the form, it goes back to its disabled form. This is a problem if you by mistake submit the form without filling all the fields. As I said, the submit button makes the form go disabled. Is there a way to either not have the form go disabled after submission or to have a "Oncheck" function so I can use $_SESSION to keep the checked attribute? Thanks in advance for all your help Code: <html> <head> <style type="text/css"> #Active { display:none; } </style> <script type="text/javascript"> function toggle() { if (document.getElementById) { if (document.getElementById("checkbox").checked == true) { document.getElementById("Active").style.display = "block"; document.getElementById("Inactive").style.display = "none"; } else { document.getElementById("Inactive").style.display = "block"; document.getElementById("Active").style.display = "none"; } } } </script> </head> <body> <?php echo "Add Field"; echo "<input type=\"checkbox\" name=\"check\" id=\"checkbox\" onClick=\"toggle();\" value=\"v\"/>"; echo "<form method=\"post\">"; echo "<div id=Active>"; if(isset($_SESSION['test']) && $_SESSION['test']!="") { echo "<input name=\"test\" value=\"". $_SESSION['test']."\" type=\"text\" />"; } elseif(isset($_POST['test']) && $_POST['test']!="") { echo "<input name=\"test\" value=\"". $_POST['test']."\" type=\"text\"/>"; } else { echo "<input name=\"test\" type=\"text\"/>"; } echo "<input type=\"text\" name=\"test2\" >"; echo "</div>"; echo "<div id=Inactive>"; echo "<input type=\"text\" disabled>"; echo "<input type=\"text\" disabled>"; echo "</div>"; echo "<input type=\"submit\">"; echo "</form>"; $test = $_POST['test']; $_SESSION = $_POST['test']; $test2 = $_POST['test2']; $_SESSION = $_POST['test2']; echo $test; ?> </body> </html> Hi thier, I am using PHP to generate a page with a list of checkbox's on it. Then I am using JavaScript to validate that the user has selected at lest 1 of the given box's before I do a post back. But I cannot get my Javascript to see my CheckBox Array. Any help you can offer would be very useful thank you all in advance. Here is my PHP code used to build to Checkbox list. Code: while ($row = mysql_fetch_array($data, MYSQL_ASSOC)) { if(is_null($row['Team_Number'])) { $str1 = sprintf('<input type="checkbox" name=\'People\' value="%s">',$row['Member_ID']); } else if($row['Team_Number'] == $_REQUEST['T']) { str1 = sprintf('<input type="checkbox" name=\'People\' value="%s" checked>',$row['Member_ID']); } else { $str1 = sprintf('<input type="checkbox" name=\'People\' value="%s">',$row['Member_ID']); } $str = sprintf('%s,%s (%s)',$row['First'],$row['Last'],$row['Nick_Name']); echo('<tr><td width ="10%" align="center">'); echo($str1); echo('</td><td width="90%" align="Left">'); echo($str); echo('</td >'); echo('</tr></td>'); } And here is my Javascript, I've added the whole function incase it's not clear just from a code segment. Code: function TeamSetup() { var required = document.form1.BowlersPerTeam.value; var total = document.form1.People.length; var Tm = document.form1.TeamName.value; var cnt = 0; var peopleIDs = ""; var test = document.getElementsByName('People'); if(isArray(document.form1.People)) { for(var i=0; i < total; i++) { if(document.form1.People[i].checked) { if(peopleIDs.length > 0) { peopleIDs = peopleIDs +","; } peopleIDs = peopleIDs + document.form1.People[i].value.toString(); cnt++; } } } else { if(document.form1.People.checked) { cnt=1; peopleIDs = peopleIDs + document.form1.People.value.toString(); } } if(Tm.length > 0) { if(cnt == required || cnt > 0) { //Hand off to next step for completeing Processing. var myForm = document.createElement("form"); myForm.action=window.location; myForm.method="Post"; myForm.appendChild(createformInput("Process","PP")); myForm.appendChild(createformInput("people",peopleIDs)); myForm.appendChild(createformInput("Team",GetQueryString("T"))); myForm.appendChild(createformInput("League",GetQueryString("L"))); myForm.appendChild(createformInput("TeamName",Tm)); document.body.appendChild(myForm); myForm.submit(); } else { alert('You need to Select at least 1 player'); } } else { alert('You need to enter the team name!'); } } hi can someone help me on my code on javascript sory im a newbie can you check this and help me what i need to do to add all the radio botton and check box. i already finish the add formula on the checkbox but i do not know how to add the two selected radio buttons to my checkbox buttons help please... for example: O selected is 80 O 60 O 60 O selected is 15 O 15 O 25 chkbox 22 chkbox 30 chkbox 8 Code: function Compute(form) { if(form.radio1[0].checked) form.text1.value=form.radio1[0].value; <---- the value of this is 80 else if(form.radio1[1].checked) form.text1.value=form.radio1[1].value; <---- 60 else if(form.radio1[2].checked) form.text1.value=form.radio1[2].value; <---- 60 else if(form.radio1[0].checked) form.text1.value=form.radio1[0].value; <---- 15 else if(form.radio2[1].checked) form.text1.value=form.radio2[1].value; <---- 15 else if(form.radio2[2].checked) form.text1.value=form.radio2[2].value; <---- 25 var a=0; if(form.chk1.checked) <----22 a = a + eval(form.chk1.value); if(form.chk2.checked) <----30 a = a + eval(form.chk2.value); if(form.chk3.checked) <----8 a = a + eval(form.chk3.value); if(form.chk1.checked && form.chk2.checked) a = eval(form.chk1.value) + eval(form.chk2.value); if(form.chk1.checked && form.chk3.checked) a = eval(form.chk1.value) + eval(form.chk3.value); if(form.chk2.checked && form.chk1.checked) a = eval(form.chk2.value) + eval(form.chk1.value); if(form.chk2.checked && form.chk3.checked) a = eval(form.chk2.value) + eval(form.chk3.value); if(form.chk3.checked && form.chk1.checked) a = eval(form.chk3.value) + eval(form.chk1.value); if(form.chk3.checked && form.chk2.checked) a = eval(form.chk3.value) + eval(form.chk2.value); if(form.chk1.checked && form.chk2.checked && form.chk3.checked) a = eval(form.chk1.value) + eval(form.chk2.value) + eval(form.chk3.value); form.text1.value=a; } hi all , I'm new to js , here is my problem : i want to make a button to toggle a checkbox , here is my code ,when i replace box.checked with boxVal it doesn't work! Code: <!DOCTYPE html> <html> <head> </head> <body> <p><button>Toggle checkbox</button></p> <input type="checkbox" id="box"/> <script> var button = document.getElementsByTagName("button")[0]; var box = document.getElementById("box"); var boxVal = document.getElementById("box").checked; button.onclick = function(){ if (box.checked==false){ //when i replace box.checked with boxVal it doesn't work! box.checked=true; } else if(box.checked==true){ box.checked=false; } } </script> </body> </html> Hi, I want to check all checkboxes using one main checkbox. Then, the checkboxes will be disabled. But then, when the user unchecks the main checkbox, it must revert all the checkboxes to be enabled. How do I do so? Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>TSPL Admin</title> <script type='text/javascript'> function checkUncheckAll(checkAllState, chkbxGroup) { // Check that the group has more than one element if(chkbxGroup.length > 0) { // Loop through the array for (i = 0; i < chkbxGroup.length; i++) { chkbxGroup[i].checked = checkAllState.checked; } document.getElementById("Checkbox7").disabled = true; document.getElementById("Checkbox8").disabled = true; document.getElementById("Checkbox9").disabled = true; document.getElementById("Checkbox10").disabled = true; document.getElementById("Checkbox11").disabled = true; document.getElementById("Checkbox12").disabled = true; document.getElementById("Checkbox13").disabled = true; document.getElementById("Checkbox14").disabled = true; } else { // Single element so not an array chkbxGroup.checked = checkAllState.checked; } } </script> <style type ="text/css"> .style42 { border-style: solid; border-width: 1px; margin-left: 10px; width: 135px; border-color: #8181F7; text-align: left; float:left; } .style45 { float: left; } </style> </head> <body> <form id="form1" method="post" action="default.aspx"> <br /> <div class="style19"> <tr> <td align="left"> <div class="style45"> <input id="Checkbox6" name="checkall" onclick="checkUncheckAll(this, grp1);" type="checkbox" />All </div> <div class="style42"> <input name="grp1" id="Checkbox7" type="checkbox" />Account Admin<br /> <input name="grp1" id="Checkbox8" type="checkbox" />Network Admin<br /> <input name="grp1" id="Checkbox9" type="checkbox" />Organization Admin<br /> <input name="grp1" id="Checkbox10" type="checkbox" />Super Admin<br /> </div> <div class="style42"> <input name="grp1" id="Checkbox11" type="checkbox" />Schema Admin<br /> <input name="grp1" id="Checkbox12" type="checkbox" />Troubleshooting Admin<br /> <input name="grp1" id="Checkbox13" type="checkbox" />Helpdesk Admin<br /> <input name="grp1" id="Checkbox14" type="checkbox" />Online Admin<br /> </div> </td> </tr> </div> </form> </body> </html> Hello all, I usually try and solve my problems by reading all post but i i'm having problem trying to find a solution my problem. I have a simple form in html which presents to the user 2 checkbox's named cash or cheque depending on which answer he gives will present to him 2 different textbox. My problem is i only want the user to be able to select one of the two checkboxes and also i would like the resulting checkbox selected to be placed on the same row. Hope this makes sense!!! I'm only a beginner in javascript and i've found some of the following code on the net but i don't know how to ammend it so that it does what i need it to do. Here is the code: Code: <HTML> <HEAD> <TITLE>Document Title</TITLE> <script> <!-- function c(){} function getresultPay(){ if(document["schDetails"]["chkCash"].checked){ document.getElementById("shwCash").style.visibility="visible" } else{ document.getElementById("shwCash").style.visibility="hidden" } } function d(){} function getresultChex(){ if(document["schDetails"]["chkcheque"].checked){ document.getElementById("shwcheque").style.visibility="visible" } else{ document.getElementById("shwcheque").style.visibility="hidden" } } //--> </script> </HEAD> <BODY> <FORM NAME="schDetails"> <table border="1" width="457"> <tr> <td width="447" align="left" colspan="5" height="12"></td> </tr> <tr> <td width="187" align="left" height="22">Amount to Pay</td> <td width="26" align="center" height="22">:</td> <td width="221" height="22" colspan="3"> <input type = text name ="txtPurCode" tabindex= 25 size="20"></td> </tr> <tr> <td width="187" align="left" height="20">How would you like to pay</td> <td width="26" height="20" align="center"><input type="checkbox" name="chkCash" value="ON" onclick = "c() ; getresultPay()"></td></td> <td width="66" height="20" align="center">Cash</td> <td width="35" height="20" align="center"><input type="checkbox" name="chkcheque" value="ON" onclick = "d() ; getresultChex()"></td></td> <td width="108" height="20" align="center">Cheque</td> </tr> <tr id="shwCash" style="visibility:hidden"> <td width="187">Cash to Pay</td> <td width="26" align="center" height="22">:</td> <td width="221" colspan="3"> <input type = text name ='txtGRTNo' tabindex = 19 size="20"></td> </tr> <tr id="shwcheque" style="visibility:hidden"> <td width="187">Cheque to Pay</td> <td width="26" align="center" height="22">:</td> <td width="221" colspan="3"> <input type = text name ='txtGRTNo' tabindex = 19 size="20"></td> </tr> <tr> <td width="447" align="left" colspan="5" height="12"></td> </tr> </table> </FORM> </BODY> </HTML> I would really appricate if someone could solve this for me. Regards |