JavaScript - Javascript To Check A Checkbox Depending On Page Content
Firstly, I'm a complete Javascript noob, so apologies if this is blindingly simple.
I'm trying to write a piece of javascript that will check a single checkbox depending on a dynamically-imported piece of information further up the page. The page will look like this: Code: <div id="title"> <p class="white">1</p> </div> <div id="question"> <p>Please indicate your area(s) of interest below:<br /> <input type=checkbox value="1" /> Option 1<br /> <input type=checkbox value="2" /> Option 2<br /> <input type=checkbox value="3" /> Option 3<br /> <input type=checkbox value="4" /> Option 4</p> </div> The variable which indicates which checkbox should already be checked is in the div 'title' in the p class 'white'. So in the case above, when the page loads the 'Option 1' checkbox should already be checked. The javascript i have, rather poorly, cobbled together so far just doesn't work, and I was wondering if anyone could point me in the direction of some useful tutorials. All the tutorials I have read so far deal with mass checking all the boxes with a button click. Similar TutorialsHello I am new at Javascript. Please help me this: I have a form and a checkbox: Code: <input name="pass" type="password" class="textfield" /> <input type="checkbox" class="check"> Click here to show/hide password If I the checkbox is checked, it will show the password in the form, it means Code: <input name="pass" class="textfield" /> if not, the input form is: Code: <input name="pass" type="password" class="textfield" /> 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, I am Aditya. I am explaining below the exact scenario where I need the help: I am developing a web application in which I need to integrate a javascript/html editor on some of the web pages and then provide 'Edit' buttons on those web pages so that users can edit the content on that partciular html/jsp page (like editing in wiki pages) and then, when they add some content and click on submit button, the new content should appear on the web page with all the formatting (i.e. bold, italics, color and so on) which was applied by user when he was entering the text. Now, I need help for the below issues: 1. Please suggest me a good javascript/html editor (freely downloadable) which I can use to integrate with my web pages. 2. Once the user has entered some content using the above javascript editor, how to make that content reach the server and update the corresponding web page. I am new to web development, so may be that these questions are too simple. But, I need some help from you. Waiting for your reply, Thanks, Aditya display text depending dropdown Hey! I am creating a new function for my website which will make it very nice I believe. But since I am no good at javascript I have got quite stuck here but I don't think it's that hard afterall. Actually I'm not completely sure if javascript is needed but please lead me in the right direction here, I'm lost. What I want to make is depending what checkboxes the visitor choose and then click "Search" he will get a certain result which is categoried under these checkboxes. I don't know what else to say but I hope you get an idea of what I mean, if not please ask and I'll try explain it better. Thankful for your help Hi guys. I'm trying to write a macro, but I'm lost. So say if I have an array of keywords to check for on a web page, Code: var keywords=new Array(); keywords[0]="You are a cat"; keywords[1]="Are you a robot"; keywords[2]="Have a good summer"; How would I make a function that checks the currently opened web page for one of the specific keywords. And if that keyword is found, then execute some action - and if not, then execute some other action? Thanks Howdy folks, New to Javascript programming. I was able to do decent javascript (getting refereces to various elements on a page and read attributes or set attributes)...but when I try to do same kind of programming in a content page, it misbehaves. For example a have a table element on the content page and button. in the onclick event of the button control, I am getting the <td> element and change its colors. but for some reason instead of changing the color of just the <td> element, the entire content page as well as Master page changes. can someone guide me please. Code: <%@ Page Title="" Language="C#" MasterPageFile="~/DMS.Master" AutoEventWireup="true" CodeBehind="Javascript_RetrievingByTagName.aspx.cs" Inherits="DMS.WebUI.Javascript_RetrievingByTagName" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server"> <script type="text/javascript"> function changecolors() { var a1 = document.getElementsByTagName("td"); var a1Length = a1.length; for (var i = 0; i < a1Length; i++) { a1[i].style.background = "#aaabba"; } } </script> <table id="mytable" border="1"> <tr> <td id="lefttd0">Left column</td> <td id="righttd0">Right column</td> </tr> <tr> <td id="lefttd1">Left column</td> <td id="righttd1">Right column</td> </tr> <tr> <td id="lefttd2">Left column</td> <td id="righttd2">Right column</td> </tr> </table> <a href="#" onclick="return changecolors();">Click to Change Colors</a> <input id="Button1" type="button" value="button" onclick="changecolors();"/> </asp:Content> thanks All, I have the following checkbox code: PHP Code: echo "<input name=\"picbigid[]\" type=\"checkbox\" value=\"$pic[src_big]\">"; I then want to select all the checkboxes so I have this code: [PHP] echo "<input type=\"button\" name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.photos.picbigid)\"> <input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.photos.picbigid)\">"; [PHP] My JS function is: Code: function checkAll(field) { for (i = 0; i < field.length; i++) field[i].checked = true ; } function uncheckAll(field) { for (i = 0; i < field.length; i++) field[i].checked = false ; } If I do it like this: PHP Code: echo "<input name=\"picbigid\" type=\"checkbox\" value=\"$pic[src_big]\">"; It works but I can't have it like that because the form posts to another PHP page that needs the input name as an array. Any ideas? Thanks! There are two pages, folder/india.html folder/usa.html and dropdown menu in each page to switch between india and usa pages. india.html page sells in INR and usa.html in USD with links to shopping carts for each currency. link1(INR) link2(USD) Except the link and the currency information(selling price), contents in both the pages are same. So here comes the problem. Google hates duplicate pages! I am looking for a solution where, a single page In That Page, a dropdown menu to select currency As the currency is selected, without page reload, the link to shopping cart and the pricing also should be changed according to the selected currency. So Single page should be able to handle both the currencies. Kindly suggest me with codes as I am new to javascript. Thanks a lot Hey all, I would like to get the id of a html check box when the client clicks it. I want to do this on the client side so i need to use java script. I want to be able to do this in both fire fox and ie window.event.srcElement works great for ie but not for firefox please do not tell me to use Event.target for firefox because that does not work. At least it is not working for me anyway. Thanks I currently have this script, I need it to check when I click the checkall button. Hm... now for some reason the select all button isnt working... Current code: Javascript: Code: <script type="text/javascript"> function checkall(chek) { for (i = 0; i < chek.length; i++) chek[i].checked = true; } </script> <script type="text/javascript"> function uncheckall(chek) { for (i = 0; i < chek.length; i++) chek[i].checked = false; } </script> PHP: PHP Code: <?php error_reporting(E_ALL); require("inc/config.php"); if (isset($_POST['del'])) { for ($count = 0;$count<count($_POST[delchk]);$count++) { $delete = $_POST[delchk][$count]; $query = "DELETE FROM persons WHERE id = '$delete'"; $result = mysql_query($query); if (!$result) { die("Error deleting persons! Query: $query<br />Error: ".mysql_error()); } } } $result = mysql_query("SELECT * FROM persons"); // Check how many rows it found if(mysql_num_rows($result) > 0){ echo "<table id=\"mytable\"> <thead> <tr> <th align=\"center\" scope=\"col\">Delete</th> <th align=\"center\" scope=\"col\">First Name</th> <th align=\"center\" scope=\"col\">Last Name</th> <th align=\"center\" scope=\"col\">High Scores</th> <th align=\"center\" scope=\"col\">Date of Entry</th> <th align=\"center\" scope=\"col\">IP Address</th> </tr> </thead> <tbody>"; echo "<form name = 'myform' action='' method='post'>"; while($row = mysql_fetch_array($result)) { echo "<tr align=\"center\">"; echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>'; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>"; echo "<td>" . $row['AddedDate'] . "</td>"; echo "<td>" . $row['Ip'] . "</td>"; echo "</tr>"; } echo "</tbody>"; echo "</table>"; echo "<hr>"; echo "<input type='submit' name = 'del' value='Delete Selected'></form>"; echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>"; echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>"; } else{ // No rows were found ... echo "No Registered Members"; echo "</tbody>"; echo "</table>"; } mysql_close($con); ?> Hello I am trying to find a way to use the check all javascript code to select all my checkboxes within a while...loop. Codes goes as follows: within the header on the top of page 1: <SCRIPT LANGUAGE="JavaScript"> function CheckAll(chk) { for (i = 0; i < chk.length; i++) chk[i].checked = true ; } function UnCheckAll(chk) { for (i = 0; i < chk.length; i++) chk[i].checked = false ; } </script> The code that displays the checkboxes, which is a page included onto page 1: echo "<a class='comp' onclick='singleHideandShow({$row['pr_id']})' style='cursorointer'>{$row['propname']}</a><p>"; $propqry = mysql_query("SELECT * FROM users WHERE propid={$row['pr_id']}"); //using the hide and show id number, once clicked it will display the below contents echo "<div id={$row['pr_id']} style='display:none;'>"; while($propf = mysql_fetch_assoc($propqry)) { if($propf['uactive'] == "yes") { $pactive = "active"; } else { $pactive = "deactivated"; } //displays the information from the DB with a checkbox echo "<form name='myform' id='formmsg' method='post' action='profile.php?paction=edit&find=none' >"; echo " <input type='checkbox' name='check_list' value='{$propf['us_id']}'> <a href='profile.php?paction=edit&pid={$propf['us_id']}' /><img src='../images/secure/edit.png' name='Edit' border='0' /></a> <a href='profile.php?paction=delete&pid={$propf['us_id']}' /><img src='../images/secure/delete.png' name='Delete' border='0' /></a><label class='cuser'> {$propf['fname']} {$propf['lname']}, ({$propf['uname']}), {$pactive}</label><br>"; } echo "</form>"; echo '<p><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.myform.check_list)"> <input type="button" name="Un_CheckAll" value="Uncheck All" onClick="UnCheckAll(document.myform.check_list)"> <p>'; echo "</div>"; I receive the following error message when I click on the Check All button. Message: 'length' is null or not an object Line: 15 Char: 13 Code: 0 URI: http://www.domainname.com/profile.ph...edit&find=none Can anyone help me figure this out? Hello Folks, Here is my dropdown menu: Code: <select name="peeps"> <option value="Frank Tompson">Frank Tompson</option> <option value="Henry Wilson">Henry Wilson</option> <option value="Bill Kent">Bill Kent</option> <option value="Jessi McDonald"> Jessi McDonald</option> <option value="John Hays">John Hays</option> How can i make it so if i click on John Hays a JavaScript pop up will say Hello John Hays, and if i click on Bill Kent a popup will say hello Bill Kent and if i click on any other name nothing happens? From my main page, I have a dropdown-login box. Is it possible login form go to my user's control panel if the login was successful, or go to the login page if it's not? Ideally, I don't want to use a re-direct. Do I need to use ajax? Thank you! Hello. The code below lets a user select a drop down option and a picture changes depending on the selection. I am trying to make it so when you select an option on the dropdown the picture shows up in the center of the screen and in the background behind the text. Here is the code. Code: <html> <head> <style type='text/css'> #txt {background-repeat:no-repeat;width:200px;height:200px;} </style> <script type='text/javascript'> function div_bg(team){ document.getElementById('txt').style.backgroundImage="url(images/" + team + ".jpg)"; } </script> </head> <body> <select name='team' onChange='div_bg(this.value);'> <option name='arsenal' value='arsenal'>Arsenal</option> <option name='liverpool' value='liverpool'>Liverpool</option> </select> <div id='txt'></div> </body> Thanks in advance!! Hi everyone. I need a Javascript code for the following: i have a checkbox on my page. when this checkbox got checked, an alert like "Are u sure?" must ask if OK or Cancel. if i click OK the checkbox becomes checked, it runs a javascript function (chkIt() )and refreshes the page. if i click Cancel the checkbox should not became checked and my function will not run. Thanks a lot. sir i have a problem in is page. i want to have a javascript function that call on checkbox on click event. if checked firstly a 125 x125 sized checkbox then i want to restrict that all checkbox that i checked later must 125 x 125 sized if i checked 250 x250 sized checkbox then create a alert message pleas select 125 x 125 sized , i also have function plz check it function validate(f) { var array = new Array(); for(var i = 0; i < document.delet.checkbox.length; i++){ var ele = document.delet.checkbox; if( ele[i].checked){ var size = document.getElementById(f).value; array[i]=size; alert(array[0]); /*if(array[0] == array[i]){ alert("size is equal"); }else if(){ alert("size is not equal"); }*/ }//end if }//end for //if(first_size != second_size){alert(second_size); return false;} }//end function this is a one row and repeated in while loop php script, row code is below. Code: <tr bgcolor="<?php echo $color;?>"> <td align="center"> <input name="checkbox[]" type="checkbox" id="checkbox" value="<?php echo $aaa['id']; ?>" onclick=" return validate('<?php echo $id; ?>');" /></td> <td align="center"><?php echo $aaa['imgSize'];?><input type="hidden" id="<?php echo $id; ?>" name="size" value="<?php echo $aaa['imgSize'];?>" /></td> </tr> <?php }//end while ?> this is a online page plz check it http://hkcsy.com/ads Hello, i want to build a tax calculator's form, i want use a checkbox, so when the checkbox "checked" then the total tax will add the "luxury goods tax" on total tax. this is my code: 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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>KasKusbay Order Calculator</title> </head> <SCRIPT language = "javascript"> var currency_rates = new Array(); currency_rates["GBP"]=0.62; currency_rates["CAD"]=0.99; currency_rates["EUR"]=0.75; currency_rates["AUD"]=0.96; function getCurrencyRates() { var currencyRates=0; var theForm = document.forms["myform"]; var selectedCurrency = theForm.elements["currency"]; currencyRates = currency_rates[selectedCurrency.value]; return currencyRates; } function addPbm() { var PbmService=0; var theForm = document.forms["myform"]; var pbm = theForm.elements["pbm"]; if(pbm.checked==true) { pbmService = [J]; } return pbmService; } function addPbm() { var PbmService=0; var theForm = document.forms["myform"]; var pbm = theForm.elements["pbm"]; if(pbm.checked==true) { pbmService = [J]; } return pbmService; } function ceiling( num, at ) { var temp = num / at; // 523/50 ==>> 10.46 return Math.ceil(temp) * at; // ceil(10.46 ==>> 11, 11 * 50 ==>> 550 } function calculateTotal() { var X; var form = document.myform; var A = form.price.value.replace(/[^\d\.]/g, ""); form.price.value = A; var B = form.shipping_cost.value.replace(/[^\d\.]/g, ""); form.shipping_cost.value = B; var F = (parseFloat(A) + parseFloat(B)) * getCurrencyRates(); var C = Number(F) * parseFloat("0.1"); form.total.value = C.toFixed(2); var G = Number(F) + Number(C); var H = Number(G) * parseFloat("0.05") form.p2.value = H.toFixed(2); var I = Number(G) * parseFloat("0.03") form.p3.value = I.toFixed(2); var J = Number(G) * parseFloat("0.2") form.p4.value = J.toFixed(2); X = Number(C) + Number(H) + Number(I) + addPbm(); form.total_IDR.value = X.toFixed(2); //exp: ceiling: document.jslearnform.total_USD.value = Math.ceil(X); //or decimal: document.jslearnform.total_USD.value = X.toFixed(2); //or even document.jslearnform.total_USD.value = Math.ceil(X).toFixed(2); } </SCRIPT> <body> <form name="myform" method="post" action=""> <table width="93%" border="0" id="form_table2"> <tr> <th width="24%"><p>Price ›</p></th> <td width="37%"><p> $ <input type="text" size="7" id="price" name="price" /> <select id="currency" name="currency" onchange="calculateTotal()"> <option value"gbp">GBP</option> <option value"cad">CAD</option> <option value"eur">EUR</option> <option value"aud">AUD</option> </select> <br /> Input Price and Currency</p></td> <td width="39%" rowspan="3"><p align="right">Duty <input type="text" style="background-color: #DCDDDD;" size="12" id="total" readonly name="total" value="" /> </p> <p align="right">Sales Tax <input type="text" style="background-color: #DCDDDD;" size="12" id="p2" readonly="readonly" name="p2" value="" /> </p> <p align="right">Income Tax <input type="text" style="background-color: #DCDDDD;" size="12" id="p3" readonly="readonly" name="p3" value="" /> </p> <p align="right">Luxury Goods Tax <input type="text" style="background-color: #DCDDDD;" size="12" id="p4" readonly="readonly" name="p4" value="" /> </p></td> </tr> <tr> <th>Shipping Cost ›</th> <td> $ <input type="text" size="7" id="shipping_cost" name="shipping_cost" /> <br /></td> </tr> <tr> <th>Kind ›</th> <td><select id="currency3" name="currency2" onchange="calculateTotal()"> <option value="value""EL">Electronic</option> <option value="value""NL">Apparel</option> </select> <br /> <input type="checkbox" id="pbm" name="pbm" /> Luxury Item</td> </tr> <tr> <th>Tax ›</th> <td><div align="center"> <input type="text" style="background-color: #DCDDDD;" size="14" id="total_IDR" readonly name="total_IDR" value="" /> </div></td> <td> </td> </tr> <tr> <th> </th> <td><div align="center"> <input type="button" value="Calculate" onclick = "javascript:calculateTotal()" /> <input type="reset" value="Reset" /> </div></td> <td><div align="center"></div></td> </tr> </table> </form> </body> </html> But i have problem with the function command, the total tax doesn't appear, seems i made wrong function code on function adPbm(). Code: function addPbm() { var PbmService=0; var theForm = document.forms["myform"]; var pbm = theForm.elements["pbm"]; if(pbm.checked==true) { pbmService = [J]; =====> anyone can help me correct this coding? } return pbmService; } Thank you for anyone could help. Hello, I'm new to Javascript, but have been programming in PHP and other web codes for 4 years now. I was wondering how you would check if a checkbox was ticked, upon its selection and then unhide HTML content, and then hide such content when deselected.. So far I have this: Code: <script type="text/javascript"> function Agree() { document.getElementById("stepTwo").className = ""; } </script> Which is called by onclick="Agree()" on the select box itself.. I understand an if statement is needed, but how would one go about doing this as my if statements do not appear to be working. Hello, I am not a coder and this has been giving me grief for days My form is located here on page three of the multi form: http://www.rocpinions.com/main-form.html I have been working on this for days and can't figure it out. I have four check boxes. Dog, Cat, Other, and None of These. If Dog Cat and Other (or any combination of) are checked that is fine but if the check box "None of These" is checked I need it to remove the checks from All other boxes. This is an example of exactly what I need my form to do jsfiddle.net/srEuM/ Does anyone know how I can accomplish this with RS Form Pro? Here is the code I am using: Code: <script type="text/javascript"> function checkI() { var checkL = document.getElementsByName('form[PetsInHouse][]'); for (var i=0;i<checkL.length-1;i++) if(document.getElementById('PetsInHouse3').checked){ document.getElementById('PetsInHouse'+i).disabled = true; document.getElementById('PetsInHouse'+i).checked = false; } else { document.getElementById('PetsInHouse'+i).disabled = false; } } </script> This is how I am calling it in the additional attributes in RS Form Pro Code: onclick="checkI();" I am currently using this code above and it only disables all checkboxes when "None of These" is checked but if I check any of the other boxes I can still check "None of These" and that is not the correct functionality. This is exactly what I need to do but in the same format I have above. http://jsfiddle.net/srEuM/ Any help would be greatly appreciated. Thank you Deziner |