JavaScript - Trouble With Array Output
I have an array, I can not figure out how to take myTemplateholder = myTemplateholder.replace(" "+children[child],children[child]+"<div id='questionRadio'><input type='radio' name='answer' value='Y' /> Y <input type='radio' name='answer' value='N' /> N <input type='radio' name='answer' value='NA' /> N/A<br /></div>");} to display to separate lines. Right now, if there is more than one value, it gets added together.
Here's the rest of the code: Code: function showText(){ // var myTemplateholder = document.getElementById('myTemplateholder').innerHTML; // document.getElementById('showMe').innerHTML = myTemplateholder; var children = new Array('Chills','Fatigue','Fever','Health History','Screening','Eye-ROS','Skin','Assessment','General Appearance','Vitals','Hearing','200','300','400','500'); var myTemplateholder = $('#myTemplateholder').text(); myTemplateholder = myTemplateholder.replace("Sections ",""); myTemplateholder = myTemplateholder.replace("History","<div id='templateHeader'>History</div>"); myTemplateholder = myTemplateholder.replace("ROS","<div id='templateHeader'>ROS</div><br>"); myTemplateholder = myTemplateholder.replace("Exam","<div id='templateHeader'>Exam</div>"); myTemplateholder = myTemplateholder.replace("ICD9","<div id='templateHeader'>ICD9</div>"); for(child in children) { myTemplateholder = myTemplateholder.replace(" "+children[child],children[child]+"<div id='questionRadio'><input type='radio' name='answer' value='Y' /> Y <input type='radio' name='answer' value='N' /> N <input type='radio' name='answer' value='NA' /> N/A<br /></div>"); } $('#showMe').html(myTemplateholder); } I'm totally stuck! Thanks for bearing with a newbie - any help would be greatly appreciated. Similar TutorialsHey. I need some javascript help. Quite confused. So I made a .js file with the following contents. function cost (){ var q = "How much do you want to spend for a Hotel?" var newb1 = prompt(q, "") var q = "How much do you want to spend for Food?" var newb2 = prompt(q, "") var q = "How much do you want to spend for Activities?" var newb3 = prompt(q, "") var q = "How much for Shopping?" var newb4 = prompt(q, "") sum2 = parseInt(newb1) + parseInt(newb3) + parseInt(newb4) document.getElementById("cost").innerHTML = "Total Price :" +sum2 } Then I inserted it into my homepage using this. <script type="text/javascript" src="hotelprice.js"></script> The prompt comes up, asks for all of the inputs. But it doesn't display the output, which should have simply added them all together. How do I fix this? Hey guys, I am pretty new at javascript but am trying to learn. Here is a problem I have encountered. I have made a form with various input fields and a set of radio buttons which change the parameters of the form. One of the radio button sets is "Search by X" and "Search by Y" I want the action parameter for the form to change depending on whethere X or Y is selected Code: <FORM name=ENTRY_FORM onSubmit="return Check();" action="pointMeTo()" method="get"> If X is selected then action="http://site.com//doSomething.action" if Y is selected then action="http://site.com//doSomethingElse.action" The way I attempted to do the above(and I am not claiming this to be most efficient, just what comes to mind at the moment) is to create three functions and two global variables. Code: var xyToggler = false; var out = ""; function toggleX(){ xyToggler = false; } function toggleY(){ xyToggler = true; } function pointMeTo(){ if(xyToggler){ out="http://site.com//doSomething.action" } else out="http://site.com//doSomethingElse.action" } Thats the javascript part, now on the html form part: Code: <FORM name=ENTRY_FORM onSubmit="return Check();" action="pointMeTo()" method="get"> <!-- Stuff --> <input type="radio" name="choose" id="X" NAME="rbXY" value="X" onClick="toggleX()" checked/><label for="X">X</label> <input type="radio" name="choose" id="Y" NAME="rbXY" value="Y" onClick="toggleY()" /><label for="Y">Y</label> The logic seems like it would work to me, but when I try it out the results that are expected to come depending on the parameters X or Y are not displayed, in fact nothing is displayed but a msg saying that there is no such link. So I am assuming that means that the function is not properly assigning the corresponding links to the action= field in the form. Many thanks to whoever can spot my error or give me some advise! All the best. Hey guys, if someone could help point me in the right direction that would be great. I cant get seem to get this array to print the numbers 1 to 15 out. When I preview I get nothing, anyone know what I am doing wrong? Thanks to those that reply. thanks got it Hi, I have 3 functions. I do not know how to have 2 functions recognize the output of an array from the other function. All of this revolves around input(text boxes). What is the array's output? In other words, how do I get the newly added textboxes recognized by the initial functions - what are they called? I have tried adding names like "No. of Hours[1]"; "No. of Hours(1)"; or,"No. of Hours1"; but none of that works. For example, one of the inputs is called "No. of Hours". That is the input textbox on the web page. When the add new rows array is invoked it adds new input textboxes and the new element is Code: el.type = 'text'; el.name = 'No. of Hours' + iteration; el.id = 'No. of Hours' + iteration; el.size = 7; cellRight.appendChild(el); What is this new input/element called so that I can refer to it for the coded function shown below? The first function provides validation for user input. Code: function checkQuarters( fld ) { var val = parseFloat(fld.value); if ( isNaN(val) || ( val*4 != Math.floor(val*4) ) ) { alert("Value must be a numeric and a multiple of 0.25"); return false; } return true; } </script> The input for the above function is Code: <td> <form style="width: 5px; height: 1px;"> <input type="text" name="No. of Hours" id="No. of Hours" style="width: 70px;" onblur="checkQuarters(this);" /> </form></td> The array outputs added textboxes thusly, Code: new_rows[row_count]['cell3']=sel; var cellRight = row.insertCell(3); var el = document.createElement('input'); el.type = 'text'; el.name = 'No. of Hours' + iteration; el.id = 'No. of Hours' + iteration; el.size = 7; cellRight.appendChild(el); new_rows[row_count]['cell']=el; row_count++; The other function uses a JQuery for a datepicker. Code: <script type="text/javascript"> $(function() { $("#startdate").datepicker(); $("#enddate").datepicker(); }); </script> The input is this. Code: <td><input style="width: 70px" type="text" id="startdate"></td> <td><input style="width: 70px" type="text" id="enddate"></td> I am lost on this and could really use some help and guidance. Thanks, John Hi How can i output two maximum numbers of an array if they are equal? var flower["roses", "violets", "buttercups", "daisies"]; var flowerAmounts[9, 8, 3, 9]; Output should be: The maximum amount of flowers is 9. There are 9 roses and 9 daisies. Hope someone can help, thanks Hello everyone I am trying to receive the output of an array from a text input here is the code that I am attempting to do but it is not displaying the output January Here is the code example [CODE] <html> <script type ="text/javascript"> <!-- function coms(str){ var myArray = new Array(); myArray[0] = ["January"]; var index = 0; for(var i = 0; i < myArray.length; i++){ index; if(myArray[i] == "January"){ index i; } if(str.length == myArray[i]){ document.write(str); } } } </script> <body> <form> Panel:<input type="text" name="panel" onkeyup="coms(this.value)" /> <div id="read"><p>Data is displayed here</p></div> </form> </body> </html> [CODE] Any tips to make this possible are welcome thank you kindly Hi i start of with a some csv data and convert this into an array. The resulting array looks like the following [["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]] But i need this to be formatted as below without the quotes around the second value. [["Apple", 64], ["Banana", 20], ["pear", 12], ["Orange", 16]] Im not sure how to achieve this. I guess i need to loop through the array and strip out the quotes but im failing to get this working. Help! I'm just a beginner with JS and having trouble with the code for my array and FOR loop. I'd like the names of these animals to be listed on separate lines. Am I way off here? I wasn't sure if the for statement had to be within a function. I've spent hours trying to figure this out and appreciate any help. <script type="text/javascript"> var animal = new Array(4); animal[0] = "dog"; animal[1] = "cat"; animal[2] = "bird"; animal[3] = "rabbit"; var counter = 0; function writeAnimal () { for (var counter = 0; counter== 4; counter++) { document.write(animal[0]); document.write("<br />"); } } </script> All I am trying to pull the data from an array and place it in a table. If I assign index 0 it works as desired. I then placed it inside a foor loop and changed the index to a variable and I get "undefined". Here is the code snipet: document.write("<table width='500'>"); document.write("<tr><td width='75%'>Product:</td><td width='25%' align='center'>Cost</td></tr>"); for (i=0; i<(itemPrice.length); i++); { document.write("<tr><td>" + itemName[i] + "</td><td align='right'>" + itemPrice[i] + "</td></tr>"); totalcost = totalcost + itemPrice[i]; } document.write("<tr><td>The total is:</td><td align='right'>" +"$ "+totalcost+ "</td></tr>"); document.write("</table>"); compared to some of the samples I have seen I apologize if this is too elementary. rneaul I am trying to sort an array of strings based on the location of a string called "this.oText.value" inside an array of strings called "aList." If "this.oText.value" comes earlier in an entry in aList (call it "a") than another entry "b", I want "a" to appear before "b". Clearly, there is something very wrong with my code. It really isn't doing anything as of right now. Code: aList.sort(sortArray); function sortArray(a,b) { if(a.toLowerCase().indexOf(this.oText.value.toLowerCase()) < b.toLowerCase().indexOf(this.oText.value.toLowerCase())) return 1; else if(a.toLowerCase().indexOf(this.oText.value.toLowerCase()) > b.toLowerCase().indexOf(this.oText.value.toLowerCase())) return -1; else return 0; } Array.prototype.each = function (fn) { this.map(fn) } This is my each function that works great in every other browser but IE. UGH! What am I doing wrong? the error points to the this in the function. Is it that IE doesn't like map? Has anyone seen this before? I thought my code was correct. Works perfect in FF, chrome and opera. the canvas text doesn't work in opera, but it does render the features so the each function is working. I'll post the code if needed, but it's huge. here's the script running. http://www.pdxbusiness.com/canvas/golf/ Short version: I'm having trouble with "moving subarrays" in a multidimensional associative array. Long version: (Yes, I know that there's technically no such thing as a js associative array and that I'm actually using a generic object.) This is one of those annoying questions for which significant code can't be shown. I'm fetching a JSON object from PHP and parsing it as multi-dimensional associative array that comes out with this "structure": Code: obj[regions][variables][years] = value; My presentation logic works fine for that. Year data is presented for each variable, and variables are grouped by region. For reference, if needed, the display is tabular and similar to this: Code: Regions | Variables | 2003 | 2004 | 2005 ========================================= | measure1 | abcd | efgh | ijkl ================================= county1 | measure2 | mnop | qrst | uvwx ================================= | measure3 | yzab | cdef | ghij ========================================= | measure1 | abcd | efgh | ijkl ================================= county2 | measure2 | mnop | qrst | uvwx ================================= | measure3 | yzab | cdef | ghij ========================================= | measure1 | abcd | efgh | ijkl ================================= county3 | measure2 | mnop | qrst | uvwx ================================= | measure3 | yzab | cdef | ghij ========================================= | measure1 | abcd | efgh | ijkl ================================= county4 | measure2 | mnop | qrst | uvwx ================================= | measure3 | yzab | cdef | ghij ========================================= My problem comes from trying to allow the option to reorganize the grouping - that is, turning it into regions grouped by variable. The display logic can handle it, but I can't get the array handling code right. The desired secondary structure is Code: obj[variable][region][year] = value; Some things I've tried: Code: /* obj is in the format of obj[region][variable][year] = value */ data_arr = new Array(); for (var region in obj) { for (var variable in obj[region]) { for (var year in obj[region][variable]) { /* fail one */ data_arr[variable][region][year] = obj[region][variable][year]; /* data_arr[variable] is undefined */ /* fail two */ y = obj[region][variable][year]; y_arr = new Array(); y_arr[year] = y; r_arr = new Array(); r_arr[region] = y_arr; data_arr[variable] = r_arr; /* only the values from the last iteration are displayed */ /* fail three */ y = obj[region][variable][year]; y_arr = new Array(); y_arr[year].push(y); r_arr = new Array(); r_arr[region].push(y_arr); data_arr[variable].push(r_arr); /* y_arr[year] is undefined */ } } } And then other permutations of those three. I could run through it easy if not needing the textual index, but that's actually part of my display data, so it has to stay. Can anyone help me with what I'm overlooking? Hi Guys, Hope you can help. How do you simply output the current url in the browser i.e if i am in google.com I want google.com outputted and so on. Also on multilanguage sites if my url is say www.mydomain.com/en/about.html or http://www.mydomain.com/en/animals/h...stallions.html I want to be able to manipulate the url so I can change en to fr for example when I jump between english and french pages. I want users to be able to switch the language of that particular page by clicking a link. I need to be able to switch segment one of the url (en,fr,de etc...)dynamically somehow instead of hardcoding a link into each page. Any ideas? THANKS Could someone take a look at this and tell me what I am doing wrong please? I have an onSubmit function that calls the following code. It works fine spitting out the return values, but the section with the "textareas for" statement is displayed after everything else. (I understand why) However, I want it to display in the order with the rest of the fields. Ideally it should be a part of the "inputArray for" statement. Is there an easy way to join these two for statements and spit everything out in the order from the original form? Thanks Code: <script> function showComposer() { var textstring = ''; var textAreaArray = ''; var textareas = document.repairform.getElementsByTagName("textarea"); var inputArray = document.repairform.getElementsByTagName("input"); for (i=0;i<inputArray.length;i++) { if (inputArray[i].getAttribute('class') == 'info') { textstring += "<table><tr><td><strong>" + inputArray[i].name + " : </strong></td>" + "<td>" + inputArray[i].value +"</td></tr></table>"; } else if((inputArray[i].getAttribute('type') == 'text') || (inputArray[i].getAttribute('type') == 'radio' && inputArray[i].checked)) { textstring += "<table><tr><td><strong>" + inputArray[i].name + " : </strong></td>" + "<td>" + inputArray[i].value +"</td></tr></table>"; } } for (i=0;i<textareas.length;i++) { textAreaArray += "<table><tr><td><strong>" + textareas[i].name + " : </strong></td>" + "<td>" + textareas[i].value +"</td></tr></table>" + '\n'; } var stringArray = textstring.concat(textAreaArray); var composer = new NKMailComposer(); composer.setRecipient(""); composer.setSubject("ESTIMATE SHEET AND REPAIR ORDER"); composer.setBody (stringArray, 1); composer.show(); document.repairform.submit(); } </script> I've tried a couple of different scripts, and I'm getting the same result, so I know I must just be missing something. I'm very new to this, so I'm not figuring it out. I want a text box to show the results of an increment counter. Every time I click a button, the number in the text box increments by one. I know it sounds simple, but it's not working. Help? My code is below: Code: <style type = "text/css"> .tbsize { width: 30px; border: 3px; padding: 10px } </style> <script type="text/javascript"> var i=0; function increase() { ++i; } </script> </head> <body> <div id = "tbsize" class = "tbsize"> <form onsubmit = "return false" action = ""> <table> <td><input type = "text" id = "addone" /></td> <td><input type = "submit" value = "Click to add One" onclick="increase()"></td> </table> </form> </div> </body> </script> I thought I knew how, but I'm unclear how to get this function to display: "document.getElementById("txtHint").innerHTML=xmlhttp.responseText;" inside the div. Will you show me please? Code: xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var div = document.createElement('div'); div.setAttribute('id', 'txtHint'); div.setAttribute("style","background-color:red;"); div.style.width = '300px'; div.style.height = '100px'; document.getElementById("txtHint").innerHTML=xmlhttp.responseText; //var txt='hello world!'; document.getElementsByTagName('body')[0].appendChild(div); document.getElementById('textHint').innerHTML=txt; } } Hi, I need to show the results in a proper manner. now I'm testing these results as a alert. But I need to show them in a table and I have to sort them according to the ascending order. Do you have any suggestions? hi, I'm a new guy to computer science and info system and I am taking an intro class to it right now. I was asked to make a simple "enter the word in the box" quiz and I can't get the output! it's driving me crazy and I get "ur score is [object HTMLInputElement]out of 3" at the bottom when I had it say, "Your score is:".. here is my html coding and no, I'm not asking for anyone to do my homework. I just want to make this work! be easy. NOTE: I'm using Apple over again because I want to get the coding down and then I will change up the quiz and the answers Code: <html> <head> <title> COMSC 100 Assignment 9 by Me </title> <script> function getInputAsText(_id){return document.getElementById(_id).value} function getInputAsNumber(_id){return parseFloat(document.getElementById(_id).value)} function setOutput(_id, _value){document.getElementById(_id).value = _value} function calculate() { // declare all variables var myResult1 var myResult2 var myResult3 var resultAsText // get variable's value myAnswer1= getInputAsText("myResult1") myAnswer2= getInputAsText("myResult2") myAnswer3= getInputAsText("myResult3") // perform concatenation if (myAnswer1.toLowerCase() == " Apple" .toLowerCase()) { score= score + 1 // got this one right myResult1= "correct" } else { myResult1 = "Wrong! It's Apple" } if (myAnswer2.toLowerCase() == "Apple".toLowerCase()) { score= score + 1 // got this one right myResult2 = "correct" } else { myResult2 = "Wrong! it's Apple" } if (myAnswer3.toLowerCase () == "Apple".toLowerCase()) { score = score + 1 //got this one right myResult3 = "correct" } else { myResult3 = "Wrong! It's Apple" } // write output value setOutput("score", "Your score is " + score + "out of 3") setOutput ("myResult1",myResult1) setOutput ("myResult2",myResult2) setOutput ("myResult3",myResult3) } </script> </head> <body> Directions:<br> Answer the three questions and press go <br> Your score will appear<br> Input values:<br> 1. A byte is how many bits?<input id="myResult1"><br> 2.Steve Jobs heads what company?<input id="myResult2"><br> 3.Bill Gates heads what company? <input id="myResult3"><br> <input type="submit" value="go" onclick="calculate()"><br> Output<br> Result #1:<input id= "myResult1" size="25"><br> Result #2:<input id= "myResult2" size="25"><br> Result #3:<input id= "myResult3" size="25"><br> Your sco <input id= "score" size="25"> </body> </html> hi, The following script is supposed to display an output of 5000 or more lines but it stucks if output is more than 3000 lines. so is it possible to achieve such a program..? or does it depend on the computer CPU? thanks PHP Code: <body bgcolor=9999cc> <script> count=0; function yaz() { document.getElementById("kutu").innerHTML=""; document.getElementById("bt").value=""; count=0; for(var a=0;a<=4;a++){ for(var b=0;b<=4;b++){ for(var c=0;c<=4;c++){ for(var d=0;d<=4;d++){ for(var e=0;e<=4;e++){ for(var f=0;f<=4;f++){ for(var g=0;g<=4;g++){ for(var h=0;h<=4;h++){ if((a<b)&(c<d)&(e<f)&(g<h)) { count++; document.getElementById("kutu").innerHTML+=count+"--"+a+".."+b+".."+c+".."+d+".."+e+".."+f+".."+g+".."+h+"<br>"; } } }} }} }} } document.getElementById("bt").value="there are.."+count+"..lines "; } </script> <center> <input type="button"id="bt" value="gooo" onclick="yaz()"> <div id="kutu"align="center" style="border-style:solid;border-width:2;border-color:blue;background-color:pink;width:400;color:green;"> </div> </body> </html> |