JavaScript - Populating A Textarea Using A Dropdown
Hi Guys,
I am trying to populate a textarea using a dropdown menu. The code I have tried doesn't seem to work, can anyone help? Code: <script type="text/javascript">function updateTextBox(val) { if(val == "1") { document.forms['myForm'].elements['myTextBox'].value = "something"; } else if(val == "2") { document.forms['myForm'].elements['myTextBox'].value = "something else"; } } </script> <form name="myForm"> <select onchange='updateTextBox(val)'> <option></option> <option value="1">1</option> <option value="2">2</option> </select> Changing this drop down should change this text area.... <textarea name="myTextBox"></textarea> </form> Any help is much appreciated. Thanks. Similar TutorialsI am fairly new at Javascript, but there is something happening I do not understand. Please someone explain it to me and help with a solution. In the function, I pull data from an URL and I have done this with several different data fields and populate the HTML input, option, and check boxes with no problem with the following method. Code: document.getElementById("othertxt").value=strValue; document.getElementById("radsig").checked=true; document.getElementById("eventtime").value=strValue; But for some reason this method for textarea does not work and I know that there is a value in strValue because check it right before the line of code. Code: document.getElementById("othertxt").value=strValue; Is textarea just treated differently? I have searched the internet and seen examples but for some reason I can not find the correct solution. Hi, I'm trying to integrate an address finder (http://www.craftyclicks.co.uk/) into my shopping cart (OsCommerce). I can get it to work but I need to add my own functionality. I'm not very experienced with JavaScript and my head has entered an infinite loop by now. The problem is that the address finder script can change the selected country in a drop-down list depending on the postcode entered by the user (using the onblur event handler). What I need it to do is to remove all other countries depending on the postcode. I can get it to remove all other countries but how do i return to the original list of countries when the postcode is changed again? Once all other counties are removed, the drop-down list will obviously only have one option left... I guess the question is also how does a function remember what it has done before, when it is called again? I have written this short test script as it is easier to work with than the craftyclicks oscommerce contribution: 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>HTML Template</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <script type="text/javascript"> //<![CDATA[ function store(element) { // store values var cl = element; var text_list = new Array(); var value_list = new Array(); var length = cl.length; for (var foo=0; foo<length; foo++) { text_list[foo] = cl.options[foo].text; value_list[foo] = cl.options[foo].value; alert("text array " + foo + " " + text_list[foo]); alert("value array " + foo + " " + value_list[foo]); } populate(cl, text_list, value_list); } function populate(element, text, value) { // populate options with previously stored values var cl = element; var length = cl.length; cl.options.length=0; for (var bar=0; bar<length; bar++) { cl.options[bar]= new Option(text[bar], value[bar], false, false); } } function crafty_set_country(code) { var cl = document.getElementById('select'); store(cl); for (var i=0; i<cl.length; i++) { if (cl.options[i].value == code) { alert(cl.options[i].value + " found"); var value = cl.options[i].value; var text = cl.options[i].text; cl.options.length=0; cl.options[0]=new Option(text, value, true, true); /* for (var j=0; j<cl.length; j++) { alert("second loop " + cl.options[j].text); if (cl.options[i].value != code) { cl.options[j] } } */ } else { alert(cl.options[i].value); } } } //]]> </script> </head> <body> <form> <select id="select"> <option value="10">ten</option> <option value="20">twenty</option> <option value="30">thirty</option> <option value="40">fourty</option> <option value="50">fifty</option> <option value="60">sixty</option> </select> <input type="button" value="remove" name="button" onClick="crafty_set_country(50)"> <input type="button" value="repopulate" name="button" onClick="crafty_set_country(100)"> </form> </body> </html> Many thanks! Martin Hi, am trying to populate my second list box from the 1st. I have done the folowing code, iam trying to call a function in the onchange event of my 1st listbox, but it does not change the URL however if i manually change my URL then the thing works fine. 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>Untitled Document</title> <SCRIPT language=JavaScript> function reload(){ var val=form1.make.options[form1.make.options.selectedIndex].value; window.location='upload.php?cat=' + val ; } </script> </head> <body> <?php $host="localhost"; // Host name $username="******"; // Mysql username $password="******"; // Mysql password $db_name="******"; $con= mysql_connect("$host", "$username", "$password"); if (!$con){ die("cannot connect"); } mysql_select_db($db_name, $con); $sql1 = mysql_query("select * from make"); $cat=$_GET['cat']; //This line is added to take care if your global variable is off if(strlen($cat) > 0 and !is_numeric($cat)){//check if $cat is numeric data or not. echo "Data Error"; exit; } if(isset($cat) and strlen($cat) > 0){ $sql2=mysql_query("SELECT * FROM model where id=$cat"); }else{$sql2=mysql_query("SELECT * FROM model"); } echo "<form id='form1' name='form1' method='post' action=''>"; echo "<p>"; echo "<label>"; echo "<select name='make' id='make' onchange='reload()'>"; while($row1 = mysql_fetch_array($sql1)){ //echo "<option value=\"${row1['name']}\">${row1['name']}</option>"; echo "<option value='$row['name']'>$row['name']</option>"; } echo "</select>"; echo "</label>"; echo "</p>"; echo "<p>"; echo "<label>"; echo "<select name='model' id='model'>"; while($row2 = mysql_fetch_array($sql2)){ echo "<option value=\"${row2['name']}\">${row2['name']}</option>"; } echo "</select>"; echo "</label>"; echo "</p>"; echo "</form>"; ?> </body> </html> Thanks Hi, Iam trying a simple fill listbox on the body load, but its not working. Can someone please help me on this 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>Untitled Document</title> </head> <script language="javascript" src="script.js"></script> <body onload="fillCategory();"> <form id="drop_list" name="form1" method="post" action=""> <label> <select name="Category" id="Category"> </select> </label> </form> </body> </html> and my script.js: Code: function fillCategory(){ addOption(document.form1.Category, "Fiji", "Fiji", ""); addOption(document.form1.Category, "Australia", "Australia", ""); addOption(document.form1.Category, "New Zealand", "New Zealand", ""); } Ok, here is my issue. I want to have an input text on the top of the page that when I type in a series of numbers and hit submit, it populates the rest of the form. Does anyone have any idea what I need to perform this action?
Hi all, I have a problem here with my coding. I want to have three levels combo box. However the second level is not populating the values, therefore the third level cannot be populated also. I have attcached my codes here. Pls have a look and tell me where is my error. Thanks, Breentha I need to pull a date from a database that is already in the correct format of YYYYMMDDHHMMSS and create a javascript new Date() so that it can be used for comparing time elapsed. I have no issue with getting the date from the db and formatting it correctly but when inserted into new Date() it doesn't work. I have it working hard coded in the javascript but would like to replace the date with a value from a database instead of having it hard coded. current javascript is: var today=new Date(); var then=(new Date(2012,03,09,12,00,00,00); var mSeconds=today.getTime()-then.getTime(); what I would like to do: var today=new Date(); var then=(new Date(<?php echo $tou; ?>); var mSeconds=today.getTime()-then.getTime(); $tou is already formatted as 2012,03,09,12,00,00,00 with commas all other variables being pulled from the db work correctly. hiii friends hw r u ??? m sachin nd currently i m a student of s\w engg. i had gone through a code which i founded on " javascriptkit.com" nd make some change according to my requirements and that code is as follows : - Code: <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Ticket Booking Page</h1> <form name="doublecombo"> <p><select name="example" size="1" onChange="redirect(this.options.selectedIndex)"> <option>Select City</option> <option>New Delhi</option> <option>Mumbai</option> <option>Kolkata</option> <option>Chennai</option> </select> <select name="stage2" size="1" onChange="redirect1(this.options.selectedIndex)"> <option value="Select">Select Cinema</option> </select> <script> <!-- /* Double Combo Script Credit By JavaScript Kit (www.javascriptkit.com) Over 200+ free JavaScripts here! */ /* this is d start of code is for selecting cinemas by selecting value of city from 1st combo box */ var groups=document.doublecombo.example.options.length var group=new Array(groups) for (i=0; i<groups; i++) group[i]=new Array() group[0][0]=new Option("Select Cinema") group[1][0]=new Option("Select Cinema") group[1][1]=new Option("HauzKhas") group[1][2]=new Option("Saket","Saket") group[2][0]=new Option("Select Cinema") group[2][1]=new Option("NaviMumbai") group[2][2]=new Option("Churchgate") group[3][0]=new Option("Select Cinema") group[3][1]=new Option("Saltlake") group[3][2]=new Option("Badabazar") group[4][0]=new Option("Select Cinema") group[4][1]=new Option("krishnapuram") group[4][2]=new Option("old market") var temp=document.doublecombo.stage2 function redirect(x){ for (m=temp.options.length-1;m>0;m--) temp.options[m]=null for (i=0;i<group[x].length;i++){ temp.options[i]=new Option(group[x][i].text,group[x][i].value) } temp.options[0].selected=true } /* this is d end of code is for selecting cinemas by selecting value of city from 1st combo box */ //--> </script> </form> </body> </html> After this code i want to populate values 3rd combo box by selecting values in the 2nd combo box ( which is cinema ) bt after applying all the posible option to solve this problm nd i m nt getting success ,so m writing to you people for ur help. i will explain u the whole matter thn u wil better understand my problm. i m making one website on multiplex( online ticket booking). so i saw the website of pvr cinemas nd i thought i wil impliment this ticket booking concept in my project, so for the fullfillness of this target i hav to take atleast 7 combo box , these combo box names are :- 1. Select City- (Like - New Delhi , Mumbai ,Kolkata ,Chennai) 2. Select Cinema(Hall Name) - ( Saket - New Delhi , South Ex. - New Delhi, Navi Mumbai - Mumbai , Salt Lake - Kolkata) 3. Select Movie- (Atleast 3 movies in each cinema nd somtimes it can more or less hall by hall ) 4.Select Date - (next 5 days of current date for advanced booking) 5. Select Time - (show time for each movie in each hall should be 3 shows, means if 3 movies are playing currently in 1 cinema thn we hav to mention show times with some difference for all 3 movies. ) 6.Select Price - ( price can b d same for all movies like - 150 (normal),250 (middle ) , 500 (high) ) 7. Select No. of Seats - ( no of seats for advanced booking is 1 to 10 for every film). your given link solved my 1st combo box problm dear bt nw facing another problm. i want to populate all the movies in 3rd combo box by selecting a cinema value in 2nd combo box . for example i selected " saket - new delhi " thn all the movies which r playing currently in this cinema should be comes in d 3rd com bo box . same as for other options south ex. - new delhi , navi mumbai - mumbai nd so on. i think this problm can be solved by three dimensional or multi dimensional arrays bt friends i dont hav much knowledge of multi dimensional arrays in javascript. if u people will help me to solve this problm thn i will be very thank full to all of you. i hav less time friends. so will u plz help me ??????? please reply me soon friends............ thank you Hi I've been working on a listbox full of employees for our company intranet. Each name has an onchange tag that calls up a floor map and that part works fine but I'd really like to populate the names from an XML list so it's easier for non-IT people to maintain. I've been doing alot of web searches on the subject which comes up alot but it's mostly just fragments of what I need being that I've got no javascript background. I found a helpful tutorial on javascriptkit but that wasn't for creating listboxes so I'm only part of the way there. This is the XML file I've got (there will eventually be 400 entries): <?xml version="1.0"?> <list> <employee> <name>##########</name> <office>1656</office> <officePH>(403)000-0000</officePH> <mobile>N/A</mobile> <email>##########</email> </employee> <employee> <name>#########</name> <office>1657</office> <officePH>(403)000-0000</officePH> <mobile>N/A</mobile> <email>###########</email> </employee> </list> The most important parts are the name which will be used as the text of the box and the office number which will determine which floorplan is displayed. I added the other information so I can potentially put it in a div display later but that can wait. Can anyone help point me to some good resources on this subject? Thanks. I am having a hard time figuring this out, I have two simple arrays and I want to populate one by asking a visitor to enter information, it goes something like this... Code: var country = new Array(5); c_list[0] = "USA"; c_list[1] = "UK"; c_list[2] = "France"; c_list[3] = "Germany"; c_list[4] = "Spain"; var president = new Array(); // Last name only president[0] = window.prompt("President of USA?", ""); // Obama president[1] = window.prompt("Prime Minister of UK?", ""); // Brown president[2] = window.prompt("President of France?", ""); // Sarkozy president[3] = window.prompt("Prime Minister of Germany?", ""); // Merkel president[4] = window.prompt("President of Spain?", ""); // Zapatero Now the question is, how do I use a simple for loop to use the names entered and populate the second array? Any help would be very kindly appreciated. Thank you Hi i have a problem, i've been trying to fix this for the whole day pls see my code below Code: for ($o = 0; $o <= $totalclass; $o++) { for($i = 1; $i <= 45; $i++) { if ($_SESSION['classification'][$o] == $i) { $sql2="SELECT ClassDesc FROM tblclass WHERE ClassID = '$i'"; $result2=mysql_query($sql2); // If successfully queried if($result2) { while ($row2 = mysql_fetch_assoc($result2)) { $ClassDesc2 = $row2['ClassDesc']; } } //echo $ClassDesc2; ?> <tr> <td bgcolor="FAFAF6" class="small" valign="top">Class <? echo $i; ?></td> <input type="hidden" name="<? echo "classid[]"; ?>" value="<? echo $i; ?>"> <td bgcolor="FAFAF6"> <textarea name="<? echo "specification[]"; ?>" COLS="50" ROWS="6" class="small" wrap="virtual" tabindex="<? echo $i; ?>"><? echo $ClassDesc2;?></textarea> <input type="button" value="Reset" onclick="window.reset();" name="reset"> </td> </tr> <? } } } i've trying to create a button or image to reset one textarea (from whole array) and so far i've been unsuccessful. i've seen this on other website and i know it is possible to do this, pls help! hey guys The first dropdown has some options as : abc(a) bcd(a) cde(b) def(b) efg(c) fgh(c) The second dropdown has : a b c On selecting abc(a) in first dropdown the 'a' must get selected in second dropdown,on selecting cde(b) second dropdown must have 'b' and so on,also the second dropdown value should be disabled(grayed out) for user.Need the code in javascript.ty in advance. Hello. I have a problem here I have this menu: Code: <script language="JavaScript"> <!-- function mmLoadMenus() { if (window.mm_menu_0516213335_0) return; window.mm_menu_0516213335_0 = new Menu("root",160,18,"Verdana, Arial, Helvetica, sans-serif",12,"#000000","#ffffff","#f7c68c","#214a6b","center","middle",3,0,1000,-5,7,true,true,true,0,false,true); mm_menu_0516213335_0.addMenuItem("Item5","location=''item5.html'"); mm_menu_0516213335_0.addMenuItem("Item6","location=''item6.html'"); mm_menu_0516213335_0.hideOnMouseOut=true; mm_menu_0516213335_0.menuBorder=2; mm_menu_0516213335_0.menuLiteBgColor='#294a63'; mm_menu_0516213335_0.menuBorderBgColor='#102939'; mm_menu_0516213335_0.bgColor='#101008'; window.mm_menu_0506211449_0 = new Menu("root",160,18,"Verdana, Arial, Helvetica, sans-serif",12,"#000000","#ffffff","#f7c68c","#214a5a","center","middle",3,0,1000,-5,7,true,true,true,0,false,true); mm_menu_0506211449_0.addMenuItem("Item1","location='item1.html'"); mm_menu_0506211449_0.addMenuItem("Item2","location='item2.html'"); mm_menu_0506211449_0.addMenuItem("Item3","location='item3.html'"); mm_menu_0506211449_0.addMenuItem("Item4","location='item4.html'"); mm_menu_0506211449_0.hideOnMouseOut=true; mm_menu_0506211449_0.menuBorder=2; mm_menu_0506211449_0.menuLiteBgColor='#294a63'; mm_menu_0506211449_0.menuBorderBgColor='#102939'; mm_menu_0506211449_0.bgColor='#101008'; mm_menu_0506211449_0.writeMenus(); } // mmLoadMenus() function MM_openBrWindow(theURL,winName,features) { //v2.0 window.open(theURL,winName,features); } //--> </script> <script language="JavaScript1.2">mmLoadMenus();</script> And I have a database with 3 fields: id, name and contents. These are the pages. Basically I want to do a select on the mysql database using PHP and populate the menu from the database. Can you tell me how to do this? Thanks Hi all, I am trying to find out if it's possible to auto populate a text input based on the value of another. e.g. there is an input called name.. the user completes this and enters something like John, then in the same form there is another input called username. Once the person enters John in the name field, the username automatically displays John. I'm not sure how doable this is, I'm not very familar with Javascript, so any advice would be greatly appreciated. Many thanks, Greens85 Hello. I would like text fields in an Adobe form to auto-populate when an item from a combo box list is selected. For example, if a user selects "shirt," then a set of text fields will auto-populate with the budget figures for a "shirt." I am not familiar with JavaScript. Would anyone be willing to explain the first steps in the project described above? Is there a way I could adapt previously-used scripts and insert my own information? Thank you in advance for your guidance. Hello, I'm trying to populate an HTML drop down list with data from an XML file but have failed miserably so far using examples I've found. Please help. Thanks, Richard http://www.javascriptkit.com/javatut...rnalphp2.shtml Anyone have a decent way of adding a fade affect to this script? Hello, I am completely new to HTML/JS/CSS, therefore I know very little. I have two drop-down prompt controls with month names. One has just one value (say "July") and the other has all the months of the year ("January".."December"). The first prompt control is hidden on the page. How do I set the default selection of the second prompt control to the value present in the first prompt control? So, when the page is run, the second prompt control should automatically show "July". I was reading up on the selectedIndex property (?), but I know that it won't work because I want Index 0 to be selected in the first control and Index 6 in the second, and I expect it to change every month (next month it will be index 7 that should be automatically selected). If it matters, I am using IE8. Thanks! Hi, I have two dropdown lists with the second one being dependent on the selection in the first. Options in list 1: 1,3 or 4 List two should be enabled when 3 or 4 is selected in list 1. So far so good, managed to get it to work with only one set of lists, but I actually have 18 of those sets in this form: Code: <select name="fw[$i]" id="fw[$i]"> <option value="1">FWH</option> <option value="3">links</option> <option value="4">rechts</option> <option value="0" selected></option> </select><br /> <select name="lie[$i]" id="lie[$i]" disabled="disabled" onchange="showBox($i,xxx);"> <option value="0" selected>---</option> <option value="1">gut</option> <option value="2">schlecht</option> <option value="3">Strafschlag</option> <option value="4">OB</option> </select> $i is my index generated by my PHP script and runs from 1 to 18. Everything works just fine as long as the index is not in play. With the index my function breaks, most probably due to my inability to extract the second parameter (xxx above) from the selection field. Here's my function, there could be something wrong there too with how exactly to specify the proper dropdown list to enable. Code: function showBox(field,val) { if (val > 1) { var box = 'lie[' + field + ']'; document.form1.box.disabled == false; } else { document.form1.box.disabled == true; } } Finally, I'm not that adept in javascript programming, more like a trial and error guy, how has hit the wall with this problem. Thanks in advance! Joe <html> <script language="javascript"> function foo() { var mainDiv = document.getElementById("my"); var div = document.createElement("div"); for(j=0;j<5;j++) { var select = document.createElement("select"); for(i=0;i<5;i++) { var option = document.createElement("option"); option.text = "hmm"; select.appendChild(option); } div.appendChild(select); } mainDiv.appendChild(div); } </script> <body> <select onchange="foo()"> <option>a</option> <option>b</option> <option>c</option> <option>d</option> <option>e</option> <option selected>-----Select------</option> </select> <div id="my"></div> </body> </html> I stuck ... but I got the solution now I want to reset on every selection ? |