JavaScript - Arrays Accepted Into A Parameter Of A Function?
Hey guys, i have a question about my array. I have an array called numberarray with 20 elements and i want it to be accepted into a function as a parameter? But i only want the first 10 to be accepted. Does anyone know exactly how i would go about doing this in my function?
Similar TutorialsHello again! Is there any way to pass a function(with its parameters) to another function using a parameter? Examples: Here is how I do it now: PHP Code: function output(text){ alert(text); } function bla(text, func){ output(text); } bla("Text is here", output) //Outputs "Text is here" I only want to pass the function to bla() as a parameter. I need something like this: PHP Code: function output(text){ alert(text); } function bla(func){ output(); } bla(output("Text is here")) //Outputs "Text is here" Is this possible in some way? Thanks in advance. Hi, I am learning javascript from a book ("Javascript and JQuery Missing Manual") and one example there sets my head spinning and I can't understand how the script works. I'd be very grateful if someone could write a few words about the aspect I don't understand. Basically it is line seven I don't understand. How is the word question tied to the items in the questions array. I understand the 0 and 1 on line eight and nine and why they focus on the first and second item in the sub-array, but still, how can the word question point to the relevant item in the questions array? Code: var score = 0; var questions = [ ['How many moons does Earth have?', 1], ['How many moons does Saturn have?', 31], ['How many moons does Venus have?', 0] ]; function askQuestion(question) { var answer = prompt(question[0], ''); if (answer == question[1]) { alert('Correct!'); score++; } else { alert('Sorry. The correct answer is ' + question[1]); } } for (i=0; i<questions.length; i++) { askQuestion(questions[i]); } Hi all I'm adding buttons to my google map through a loop. I pass some parameters, and the addButtons() function creates the buttons. How can I pass the "task" for the button to the function? In the addDomListener line below, I'm now writing the function name literally, I'd like find a way with a parameter. Thank you.. Code: // The loop... for (i=0;i<=4;i++) { oCC = addButtons(aCtrls[i],'btnContainerChild'); ... } // The function... function addButtons(cButtonID,cClassName) { var oButton = document.createElement('DIV'); oButton.className=cClassName; oButton.id=cButtonID; //... other settings here... // ===> Here, I'd like to set the click function through a parameter google.maps.event.addDomListener(oButton, 'click', function() {gmapSetCenter(defaultCenter)}); Please advise me: I've successfully called a variety of functions that have no parameter value such as: function expand() //expand() in this line is merely an example But I'm unsuccessful calling a function with a paramter value such as: function expand(id) Here is the the call line: document.addEventListener("DOMContentLoaded", expand(id) , false); Here is the Function: function expand(id) { for (var i = 1; i<=10; i++) { if (document.getElementById('smenu'+i)) { document.getElementById('smenu'+i).style.display='none'; } } if (id) { id.style.display='block'; } } I have a good understanding of javascript although I am new to working xml. So I am having a problem getting javascript to recognize that my parameter value in my function call is an attribute in my xml. I called the js function like this: Code: <a href="#" onClick="ebookCategory(marketing);">Marketing</a> Here is my js function: Code: var category; function ebookCategory(category) { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","ebook.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.write("<table border='1'>"); var x=xmlDoc.getElementsByTagName(category); for (i=0;i<x.length;i++) { document.write("<tr><td>"); document.write(x[i].getElementsByTagName("bookCover")[0].childNodes[0].nodeValue); document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue); document.write(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue); document.write("</td><td>"); i++; document.write(x[i].getElementsByTagName("bookCover")[0].childNodes[0].nodeValue); document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue); document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue); document.write(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue); document.write("</td></tr>"); } document.write("</table>"); } // end ebookCategory() Some of the aspects in this function are new to me, xml stuff. Here is my xml setup: Code: <ebook category="marketing"> <bookCover><img src="/millionaire/ebooks/covers/miracle2.jpg" height="150" width="150" /></bookCover> <title>Marketing Miracle</title> </ebook> I appreciate any help in the matter 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> window.onload = function(){h = parseInt(document.getElementById( "div1" ).style.height);} var t; function addHeight(openAccordionID){ clearInterval(t); document.getElementById(openAccordionID).style.height = h+"px"; h+=1; t = setInterval("addHeight('"+openAccordionID+"')",10); for(var x=1; x<h; x++) if(h%(x*100)==0){clearInterval(t);} } </script> </head> <body> <div id="div1" onclick="addHeight(this.id)" style="width: 200px; height: 20px; background-color: yellow;">Increase Height</div> </body> </html> How can I still use the parameter "openAccordionID" in the blue-highlighted part? I tried, but it failed. public classTestGeometricObject{ /** Main Method */ public static void main(String[] args) { //Declare and initialize two geometric objects GeometricObject geoObject1 = new Circle(5); GeometricObject geoObject2 = new Rectangles(5,3); System.out.println("The two objects have the same area? " + equalArea(geoObject1, geoObject2)); //Display circle displayGeometricObject(geoObject1); //Display rectangle displayGeometricObject(geoObject2); } /**A method for comparing */ public static boolean equalArea(GeometricObject object1, GeometricObject object2) { return object1.getArea()==object2.getArea(); } /**A method for displaying a geometric object*/ public static void displayGeometricObject(GeometricObject object){ System.out.println(); System.out.println("The area is " + object.getArea()); System.out.println("The perimeter is " + object.geetPerimeter()); } } I have a simple function to display an image when called, but if I try to rewrite the function to take the image as an argument, it stops working. I've looked at other function examples on the web but can't figure out what I am missing. Can anyone help? works: Code: <html> <script language="JavaScript"> var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>'; function FlashImagesThenWait() { document.getElementById("first").innerHTML = ImagePlusSign } setTimeout(FlashImagesThenWait, 1500); </script> <body>Hello. <div id="first"> </div> </body> </html> does NOT work : Code: <html> <script language="JavaScript"> var ImagePlusSign = '<img src="plus.jpg" name="slide" width="65" height="50"/>'; function FlashImagesThenWait(z) { document.getElementById("first").innerHTML = z } setTimeout(FlashImagesThenWait(ImagePlusSign), 1500); </script> <body>Hello. <div id="first"> </div> </body> </html> Heya guys I'm looking for someone with a vast knowledge of Javascript... by any chance someone over here has some time to help a newby out? (I think 10 minutes should be enough) <INPUT TYPE="button" NAME="addToArray " VALUE=" ATA " OnClick="addToArray() "> <!--Add To Array --> <INPUT TYPE="button" NAME="clearArray" VALUE=" CA " OnClick="clearArray()"> <!--Clear Array --> <INPUT TYPE="button" NAME="showArray" VALUE=" SA " OnClick="showArray() "> <!--Show Array --> These are my buttons. What I want them to do is simply add something to an array, clear the array and show them on my screen. Hello there, I was having some trouble with 2D arrays (or array of arrays). Essentially, the array has 100 rows, with two columns. The first column of every row holds a name, and the second holds a sales amount. With the use of a do while loop, the user can continuously add up to 100 names and sales amounts. After all the information the user wishes to add is stored into the 2D array I'm attempting to pass that very same 2D array as a parameter to a function called printRow as can be seen in the code below: Note: the function call and the actual function are found in two separate javascripts. Code: var salesPerson=new Array(100) for (i=0; i <=100; i++) { salesPerson[i]=new Array(2); } var x = 0; do { salesPerson[x][0] = getName(); salesPerson[x][1] = getSales(); x++; }while(x != 100 && confirm("Enter more employee information?")); printRow(salesPerson[][]); Code: function getName() { var nameEntered = prompt("What is your first name?"); return nameEntered; } function getSales() { var error; var salesEntered; do { salesEntered = prompt("What were your sales?"); error = false; if (isNaN(salesEntered) || salesEntered == null || salesEntered == "") error = true; }while(error); return salesEntered; } function printRow(salesPerson[][]) { for (i =0; i<salesPerson.length; i++) { document.write(salesPerson[i][0] + " " + salesPerson[i][1]); } } At the moment I'm only looking to print the contents of the 2D array that I pass as a parameter to the document. As is, the javascipt doesn't seem to execute at all, it worked fine up until I added the printRow function call and function which leads me to believe I may not be passing it as a parameter correctly. Any tips on how to do this correctly would be greatly appreciated! I need to loop the alphabet and numbers 0-9 to initialize a few thousand arrays. This is for my site and is truly needed. http://www.thefreemenu.com I currently have every array written out and it takes up to much space in my .js file. The majority of my variables are empty but necessary and need to be there (including empty) for my site to work properly. Question is the last part Here's where I'm at. Code: var NewVarLetterOrNum = "a"; eval("_oneofseveralnames_" + NewVarLetterOrNum + "='this part works';"); alert(_oneofseveralnames_a); This creates the variable _oneofseveralnames_a='this part works' Code: var newArrayLetterOrNum = "a"; eval("_oneofseveralnames_" + newArrayLetterOrNum + "= new Array();"); alert(_oneofseveralnames_a) This creates the Array _oneofseveralnames_a=new Array(); and all the values in the array are null, but, now a variable like _nl_a[1]='something' can be used elsewhere because the array exists. This is all that is necessary for now because I can probably set all the variables to be blank with something like Code: i=1 while(i<=20){ _oneofseveralnames_a[i]="1-20"; i++ } alert(_oneofseveralnames_[20]); So now you have what I came to understand in the first few hours. Now to the hard part : ( I can't make multiple array's dynamically. I dont' know if its because I don't understand loops or arrays or what and its very fustrating. As for any answer you might be so kind as to provide, if you could dumb it down that would be greatly appreciated. Code: var newArray =new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z') i=1 while(i<=26){ eval("_nl_" + newArray[i] + "= new Array();"); i++ } alert(newArray[1]) // Is b, but alert(_nl_b) //I can't get _nl_b to exist, I tried everything including taking away the quotes around the letters in every test */ var _nl_a =new Array() var _img_a =new Array() var _h_a =new Array() var _r_a =new Array() var _m_a =new Array() var _yt_a =new Array() var _i_a =new Array() The above arrays are all the array _name_ parts I need but for example, a has 10 parts, a,p2_a,p3_a,.. p10_a. I need 10 pages for each letter of the alphabet and numbers 0-9 and a special all1, p2_all1 ... p10_all1. Overall 2200 arrays that need to be declared. Currently they are all written out. /* Hi I have this code which works fine: Code: <td class="Content_TD" onclick="_gaq.push(['_trackEvent', 'navigation ', 'link', 'blocks']); window.open('http://www.google.dk/');"> But I dont now the parameters instead of window.open, if I want it to open on top. Can somebody please help me? Thanks in advance Claus I have to use get method (instead of post) for form action but dont want to show the url parameter in the action page url. Anyway to hide or encrypt a form input value after it goes into my database? Code: <form action="actionpage.cfm" method="get"> <input type="text" name="myValue"> <input type="submit" name="submit"> </form> Hi I am in doubt if im doing this correct. I have a TD which I have a onclick and a google event tracking code on. But om not shure if I have set it up correct. The () and the , might be wrong..? <td class="Content_TD" onclick="(window.open('http://www.google.com/')), (_gaq.push(['_trackEvent', 'navigation', 'link', 'event']));"> Thanks in advance! Hello there, Could someone tell me how a javascript can get a cookie and use it as a parameter like www.mysite.com?search=COOKIEDATA Thanks you (a) I've been staring at this same problem for over a week now. I've worked around it as best as i can but i think it's time to ask someone else for input. I'm trying to pass a value thru an ajax parameter.. that's all. it SHOULD be easy in theory. what i want to do is create a jscript variable then pass that variable as the value for a parameter. php then converts that value to something it can use to finish the rest of my code. As i said it's hindering my webpage progress and i would like to get it fixxed soon so any help would be appreciated. Code: function getSelection() { var selection=document.getElementsById("SelectedItem"); var xmlhttp;//create a var for the obj if (window.XMLHttpRequest)//if requesting an obj... { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your browser does not support XMLHTTP!"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) { var text = xmlhttp.responseText; addNode(text); } } xmlhttp.open("GET","battle_nin.php?selection",true); xmlhttp.send(null); } //the php snippet $currentjutsu=$_GET[selection]; When calling a javascript file I have written <script type = "javascript" src = "mycode_2010.js"> However, I would like to replace 2010 with a variable so that for example I can detect the year and load the appropriate code e.g. mycode_2011.js rather than writing a new hard coded html page. Is it possible please? I want a URL parameter, like http://www.thatswhyimbroke.com/#tab-1 to be added when a link is clicked. So for example, when I click one of the menu tabs on http://www.thatswhyimbroke.com/, I want a #tab-1 to be added at the end of the URL. If you look now, clicking a new tab adds nothing to the URL because it's pre-loaded. Please let me know if you need more explanation, I'm not sure i'm explaining it correctly. Thanks for any help! I could not figure out the answer, and could not find the answer from Google. I have following object PHP Code: ; var obj = function() { ; this.method_1 = function() { ; return this.method_2 } ; this.method_2 = function() { ; return 'my parameter' } } It works. If I want method_2 having parameter, I write it this way: PHP Code: ; var obj = function() { ; this.method_1 = function() { ; return this.method_2 } ; this.method_2 = function(para) { ; return para } } ; var newObj = new obj() ; window.alert(newObj.method_2('my parameter')) It works and returns "my parameter". If I want method_2's parameter to be assigned by method_1, I write it this way: PHP Code: ; var obj = function() { ; this.method_1 = function() { ; return this.method_2('my parameter') } ; this.method_2 = function(para) { ; return para } } It does not work. So, the question is how to assign a parameter of a method by another method? Thanks a lot. Hi, Im not very good with javascript so I appologize is this is a stupid question. In the code of my gallery software I have this line: <h2 id="item-title">{$theme.item.title|markup|default:" "}</h2> This displays the title of the image. I dont actually want to display the title of the image. However, when I delete this line, the gallery no longer works (in the view photo page, the javascript image loader just says loading). Is there a way to keep this required parameter in, so that my gallery works, but stop it from being displayed on the page? Here is a link to the page (currently ive left the line in) http://www.zombiemod.com/rm/nina2/main.php?g2_itemId=13 |