JavaScript - Checkbox Selection
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 Similar TutorialsNeed a javascript function... If a checkbox is checked, then corresponding dropdown selection is must... Please help me with this... Thanks Reply With Quote Hi all, I have a form where I want a single checkbox to toggle the selected/unselected status based on the status of that master checkbox. I have a working script that accomplishes this, but I'm having a problem now that I'm submitting the checkbox as an array. Here is the JS : Code: function checkAll() { if(document.resultForm.master.checked== true) { for(var i=0; i < document.resultForm.choices.length; i++) { document.resultForm.choices[i].checked=true; } } else { for(var i=0; i < document.resultForm.choices.length; i++) { document.resultForm.choices[i].checked=false; } } } Here is the form that it currently works with : Code: <input type="checkbox" name="master"> <input type="checkbox" name="choices" value="choice 1"> <input type="checkbox" name="choices" value="choice 2"> <input type="checkbox" name="choices" value="choice 3"> <input type="checkbox" name="choices" value="choice 4"> And this is the form that I want it to work with instead : Code: <input type="checkbox" name="master"> <input type="checkbox" name="choices[]" value="choice 1"> <input type="checkbox" name="choices[]" value="choice 2"> <input type="checkbox" name="choices[]" value="choice 3"> <input type="checkbox" name="choices[]" value="choice 4"> Any help would be greatly appreciated! 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 Does anyone know anything about selecting text in an editable iframe? The following code kind of works in FireFox (it's a little buggy), but doesn't work at all in anything else. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>ADVANCED EDITOR TEST PAGE</title> <script type="text/javascript"> function framego(){ document.getElementById("frame").contentDocument.designMode = "On"; document.getElementById("frame").contentWindow.document.body.innerHTML = "TEST [here] ING"; } function selectArea(){ o=document.getElementById('frame'); if(o.contentWindow.getSelection) sel = o.contentWindow.getSelection(); //FF else sel = o.contentDocument.selection.createRange(); //IE range2=sel.getRangeAt(0); var range = document.createRange(); range.setStart(sel.anchorNode,5); range.setEnd(sel.anchorNode,11); //remove the old range and add the newly created range sel.removeRange(range2); sel.addRange(range); o.focus(); } </script> </head> <body onload="framego()"> <iframe id="frame"></iframe> <button onclick="selectArea()">TEST</button> </body> </html> It's supposed to select the word "Here" in the iframe. A million thanks to anyone who can point me in the right direction on this one. :-) Hello, Is there anyway to prevent images, words, divisions, etc from being selected? Also if something is selected is there a way to deselect? Thanks! ok i have to selection boxes and they need to read each other and input either a yes or a no into a text form i would really appreciate some help thanks heres what i have if it helps Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" /> <title>Galvanic Corrosion Chart</title> </head> <body> <br /> <form name="gcc"> <div align="center"><a href="website.html"><img style="border: 0px solid ; width: 278px; height: 50px;" alt="" src="pics/LOGO.png" border="0" height="50" width="278" /></a><br /> </div> <p style="text-align: center;" align="center"><font size="+2"><span style="font-family: Century Gothic;"><font style="text-decoration: underline;" size="+3"> <font face="Futura Bk BT">Galvanic Corrosion Chart<span style="text-decoration: underline;"><span style="text-decoration: underline;"><span style="text-decoration: underline;"><span style="text-decoration: underline;"><span style="text-decoration: underline;" /></span></span></span></span></font></font></span></font><font face="Futura Bk BT"><br /> </font> </p> <br /> <table style="text-align: left; margin-left: auto; margin-right: auto;" height="119" width="610"> <tbody> <tr> <td style="vertical-align: top; text-align: center;" align="center" valign="middle"><font face="Futura Bk BT"><font size="+1"> <font size="+1"> Material 1</font></font><br /> <select name="mat1"> <option>Zinc Plating</option> <option>Zinc Die Casting</option> <option>Galvanize</option> <option>Tin-Zinc</option> <option>Cadmium-Zinc Solder</option> <option>Aluminum(Clad.1100.3003.5052.6160)</option> <option>Cadmium Plate</option> <option>Aluminum Castings</option> <option>Carbon & Alloy Steel, Cast Iron</option> <option>Aluminum(2024.2017.7075)</option> <option>Lead</option> <option>Lead-Silver Solder</option> <option>Tin-Lead Solder</option> <option>Tin Plating</option> <option>Chromium Plate</option> <option>Stainless 18/2</option> <option>Copper & Alloys</option> <option>Stainless 18/8</option> <option>Silver Solder</option> <option>Monel</option> <option>Nickel Plate</option> <option>Titanium</option> <option>Silver Plate</option> </select> <br /> </font></td> <td colspan="1" rowspan="1" style="vertical-align: top; text-align: center;" valign="middle"><font face="Futura Bk BT"><font size="+1"> Material 2</font><br /> </font> <select name="mat2"> <option>Zinc Plating</option> <option>Zinc Die Casting</option> <option>Galvanize</option> <option>Tin-Zinc</option> <option>Cadmium-Zinc Solder</option> <option>Aluminum(Clad.1100.3003.5052.6160)</option> <option>Cadmium Plate</option> <option>Aluminum Castings</option> <option>Carbon & Alloy Steel, Cast Iron</option> <option>Aluminum(2024.2017.7075)</option> <option>Lead</option> <option>Lead-Silver Solder</option> <option>Tin-Lead Solder</option> <option>Tin Plating</option> <option>Chromium Plate</option> <option>Stainless 18/2</option> <option>Copper & Alloys</option> <option>Stainless 18/8</option> <option>Silver Solder</option> <option>Monel</option> <option>Nickel Plate</option> <option>Titanium</option> <option>Silver Plate</option> </select> </td> </tr> <tr> <td colspan="2" rowspan="1" align="center" valign="top"><font face="Futura Bk BT" size="+1">Acceptable</font><br /> <font face="Futura Bk BT"><input name="AR" readonly="readonly" /></font></td> </tr> </tbody> </table> </form> </body> </html> i have a selection menu here. what i would like to do is that when i select "not copy" it will not show and not copy the contents of "todo" textfield. but when i select any of the "copy text" it will show and copy the contents of the "todo" text. help plsssssssssssssssssss <!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>Untitled Document</title> </head> <body> <form id="form" name="form" method="post" action=""> <p>Phone Number: <input type="text" name="Name" /> </p> <p>Location: <select name="selector" > <option>copy text</option> <option>not copy text</option> <option>copy text</option> <option>copy text</option> </select> </p> <p>To Do Today: <input type="text" name="todo" /> </p> <p> <input type="button" value="Copy" /> </p> </form> </body> </html> Hi all, I'm trying to do something with a WYSIWYG editor. Below is a piece of code that takes selected text and wraps various BBCodes around it. The code almost works... but I want a few changes and I hope someone has an idea for me. Note: the var range = ed.selection.getRng(true); line gets a W3C compatible range containing the current selection - even under MSIE. The editor takes care of this. Note: The var elem = ed.dom.create('span', false, ' '); line creates a SPAN element with the initial content " " and returns it's element in "elem". Code: var ed = (tinymce.activeEditor || opener.tinymce.activeEditor); if(ed) { var sel = ed.selection.getContent(); var range = ed.selection.getRng(true); var elem = ed.dom.create('span', false, ' '); elem.innerHTML = bbopen + sel + bbclose; range.deleteContents(); range.insertNode(elem); ed.selection.select(elem); ed.focus(); return; } This code works, but not exactly as I want. For example, if I type "This is a test" and wrap code tags around it, I get this: But what I WANT is this: Also, if I select no text and hit the BBCode button, I get the open tag and the close tag, all highlighted. What I want in this case is the open tag, the close tag and the cursor IN BETWEEN them ready for text insertion. I'm thinking that I can take the range start and end offsets and move them to get what I want, but so far I've failed. One thing I DON'T want to have to do is create THREE spans... one for the open, one for the selection and one for the close. Trying to avoid that if possible. Any help will be greatly appreciated! Thanks! -- Roger Hi, I have a page with a drop down and there are external js file that are included in the page which define handlers and other actions for onchange of this drop down. So if the selection in the drop down changes, we display a different color map (based on the selection). Now I have to pre-select an option on a specific page (which has been completed) and the drop down shows the correct selection, but the event is not fired (that changes the map color). Any idea what I need to do? Thanks I am trying to change text in one cell of a table when a user selects an item from a select menu. The data for the select menu is being pulled from a database (which works fine). The select menu is displayed as expected; however the text in the div tag always comes up as "undefined" The code below is part of a PHP script (thus \" instead of ") I would like to employ a method that does not require extra Javascript in the <head> section as in this instance the code is being output from an include that is called long after the <head> area of the document is processed. Here is what I have Code: <tr><td><select name=\"race\" onchange=\"document.getElementById('ExtraInfo').firstChild.nodeValue = this.options[this.selectedIndex].extrainfo\"> <option selected=\"selected\" value=\"\">Select Race</option>\n"; $sql = mysql_query("SELECT * FROM races");$i=1; while ($row = mysql_fetch_array($sql)){ $output .= "<option value=\"$i\" ExtraInfo=\"{$row['des']}\">{$row['race']}</option>\n"; $i++; } $output .= "</select></td> <td><div id=\"ExtraInfo\">Select Race</div></td></tr> Any help will be greatly appreciated. I found a solution to my problem: by replacing: Code: <select name=\"race\" onchange=\"document.getElementById('ExtraInfo').firstChild.nodeValue = this.options[this.selectedIndex].extrainfo\"> with this: Code: <select name=\"race\" onchange=\"document.getElementById('ExtraInfo').firstChild.nodeValue = this.options[this.selectedIndex].getAttribute('extrainfo')\"> Ok, I am working on a webpage and on one page section I have 3 random facts. Now, having to change the three random facts to three different random facts every week will be a annoyance. So I need help in creating a code that will allow 3 random facts to chosen every Sunday(for example). Is it possible to do this? Also I can not do this in php. Hey guys i have been fighting with this issue for some time so I figured i would join this forum and ask for some help for once. Here is what i am trying to accomplish. I have a drop down menu in place to select a month and a year for our owners to select and check their availability calendar. The issue is that i am tired of every month/year going into the code to change the preselected month/year setup less i hear screaming to high heaven from the owners that "The month isnt the current month" constantly. Here is the code i am currently using. <p align="left"><font face="Verdana"> <!--webbot bot="Validation" b-value-required="TRUE" b-disallow-first-item="TRUE" --> <select size="1" name="arrivemonth" value="%%arrivemonth%%"> <option>Select Month</option> <option value="01" SELECTED>January</option> <option value="02">February</option> <option value="03">March</option> <option value="04">April</option> <option value="05">May</option> <option value="06">June</option> <option value="07">July</option> <option value="08">August</option> <option value="09">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select></font></TD> </TR> <TR> <TD bordercolorlight="#808080" bordercolordark="#808080"> <font face="Verdana"> <!--webbot bot="Validation" b-value-required="TRUE" b-disallow-first-item="TRUE" --><select size="1" name="arriveyear" value="%%arriveyear%%"> <option>Select year</option> <option value="2004">2004</option> <option value="2005">2005</option> <option value="2006">2006</option> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010" SELECTED>2010</option> <option value="2011">2011</option> </select> All i am trying to accomplish is a code that will check the current month and year then preselect the value in the drop down automatically. This information is then fed to a .CGI script to pull data from our local server for their condo availability. Any ideas anyone? I have 2 textareas, 1 which will be vissible, another that will be hidden. I have a function that mirrors (not in reversed lettering) the first textarea's input to the second. Code: function copy_data(val){ var messageText = document.getElementById(val.id).value document.getElementById('message').innerHTML = messageText; } So that copies from : <textarea name="text_message" rows="4" id="text_message" style="direction:ltr;" onkeyup="copy_data(this)"></textarea><br/> To: <textarea name="message" rows="4" id="message" style="direction:ltr;"></textarea> Then I have another function which when I select text in the "text_message" textarea will apply a tag to it, however I don't want this tag to be vissible in the "text_message" textarea but rather in the "message" textarea. Code: function formatText(el,tag){ var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd); if(selectedText!=''){ var newText=''+tag+''+selectedText+''+tag+''; el.value=el.value.replace(selectedText,newText) } } This effects from this checkbox: <input type="checkbox" id="bold1" name="bold1" onclick="formatText(message,'#28D')" checked="checked" > Could anyone please help me, I think I may have missed something somewhere, because it works if I point it back to "text_message" Hello, I want to create a page that allows the user to select 2 images as options, when they click one, a new page is dynamically loaded with a list of options they can choose again (like a sublevel of the image they clicked on). From there it will just continue moving down like a hierachy Can someone help me with the basic script forthis to work so i can mess with it and edit it to my needs ^ Thank you in advance Basically what I have here in the following code is 15 random numbers that I am supposed to use the bubble and selection sort algorithm to sort. How can I go about getting the numbers to be sorted using selection sort I have already done the bubble. Thanks Code: <html> <body> <script language ="javaScript"> var array = new Array(15); function genNumbers(listbox){ var i; for(i= 0; i < array.length; i++) { array[i] = Math.random()*15; array[i] = Math.round(array[i]); } updateList(listbox); } function sortNumbers(listbox) { var x, y, holder; for(x=0; x < array.length; x++) { for(y=0; y< (array.length-1); y++){ if(array[y] > array[y+1]){ holder = array[y+1]; array[y+1] = array[y]; array[y] = holder; } } } updateList(listbox); } function updateList(listbox) { var i; for(i = 0; i< array.length; i++){ if(listbox.options[i] == null) { listbox.options[i] = new Option(array[i]); } else{ listbox.options[i].text = array[i]; } } } </script> <form> <center> <select name = "ranlist" size "10" style = "width: 100px"> </select> <br><br><br> <input type = "button" value = "Generate" onClick = "genNumbers(this.form.ranlist);"> <input type = "button" value = "Bubble Sort" onClick = "sortNumbers(this.form.ranlist);"> </form> </body> </html> Very, VERY new to javascript, hopefully this question isn't too bad.. So I'm looking at an example of randomly changing a picture through a function, I want to understand *why*, not simply know how.. The confusing code for me is: Code: while (that.src.indexOf(myImages[newImgNumber]) != -1) The other code is irrelevant. My question is, why would testing a current image with a new image result in -1 if they are the same? To me, it seems like it should be !=0? I mean, if I tested if a number was the same as another number, I would expect a result of 0 by subtracting. Is it not subtracting index position to find out if they're the same? Does it simply just return -1 if they are the same, and I'm over thinking it? Thanks for any help. 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. as I have no idea if this will boil down to a JS problem, or a CSS problem, I've posted this to both forums. If some enterprising moderator would like to consolidate them, have at ye! Using javascript I've made a simple drag and drop interface for a backend component. Elements are dragged from a list at right into a main area. The elements are organized as a list (<ul><li>). Each has a mouse down listener which flicks on the visibility of a div (that mirrors the appearance of the named <li>) and then follows the mouse as long as it is needed. All is peachy... ... BUT THEN !!! ... As one might predict, the dragging action selects whatever content comes between the list item where the drag started and the placement where the drag ends. It's ugly and distracting; furthermore it changes my nice 'pointer' cursor to a 'text' cursor. I don't want to have to disable selecting for the entire page (nor do I know of a slick, elegant way to accomplish this) and I've tried all sorts of other hackery: cover up divs were especially fail-tastic. Hoping someone out there's cleverness gland can help me with this one. Excerpt from the site below to give an idea of layout: elements of the orange list at right get dragged into the main area in the middle I can't link to the site directly, sorry, I don't have a version ready that doesn't modify existing databases. Thanks, -S as I have no idea if this will boil down to a JS problem, or a CSS problem, I've posted this to both forums. If some enterprising moderator would like to consolidate them, have at ye! CSS thread link Hi all, I am working on code to wrap BBCodes around a user selection, then leave the selection selected for possibly adding more BBCodes around the same selection. My problem is that there seems to be several different ways to do it and I can't figure out which one is "the best". Now, I can't use the "standard" way of doing this because I am working with TinyMCE. I know there are countless examples on how to do this with textareas... that is NOT what I need. TinyMCE can give me a selection RANGE, a selection NODE or a selection OBJECT. Note that if I use TinyMCE's range, it internally "normalizes" the range to be a standard W3C range, regardless of browser. I've created a new range, extracted the selection from the user's range, created elements for the open and close BBCode, inserted the elements into the range and finally set the user's selection to the new range. Works fine. I also tried taking the selection node (which is an element), then simply used "innerHTML" to add the opening and closing tags, then set the node to the modified node. Works fine. I've also used the editor's built in "selection.getContent()" and "selection.setContent()" calls and THIS also works. I've even tried using the DOM "surroundContents()" function, then used a regex to change the < and > into [ and ]. Freaky... but that works too. So, my dilemma is... which method should I use??? To recap, here's what I want (blue represents selected text): This is a test sentence. (user selected some text) This [bbcode] is a test [/bbcode] sentence. (text is wrapped and remains selected). This [bbcode][newcode] is a test [/newcode][/bbcode] sentence. (user clicked another bbcode button). If no text is selected, then simply the open and close tags are inserted, with the cursor blinking in between them.... [bbcode] | [/bbcode] (pipe symbol represents the cursor) Any guidance will be greatly appreciated! -- Roger |