JavaScript - How Can I Call A Variable From A Pieced Together String?
I'm sure it's just a syntax problem. You can ignore most of this function because most of it works. The part I'm asking about is the "coordinates" variable 4th line from the bottom. The "office3" variable is a dynamically populated office number. I have the office numbers all defined as variables somewhere else but since I can't just name the variables as numbers I had to put a $ in front of them. Basically I'm trying to call the office number variable, $1656 for example. So "coordinates" is meant to be equal to "$+the chosen office number".
Problem is coordinates is coming up as literally "$1656" instead of the value of $1656. Sorry if that was confusing, can anyone point out the problem? function display(name3,office3,officePH3,mobile3,email3) { var instruct="<span class='style4'><strong>"+"LOOK FOR THE FLASHING COMPASS ROSE ON THE MAP"+"</strong></span>"+"<br /><br />"+"<span class='style4'><strong>"+"HOVER ON A PRINTER TO VIEW ITS INFO OR CLICK TO MAP IT"+"</strong></span>" var info_output="NAME: "+(name3)+"<br /><br />"+"OFFICE: "+(office3)+"<br /><br />"+"DESK PHONE: "+(officePH3)+"<br /><br />"+"MOBILE: "+(mobile3)+"<br /><br />"+"EMAIL: "+"<a href=mailto:"+(email3)+">"+(email3)+"</a>"+"<br /><br />"+(instruct) document.getElementById("info").style.visibility="visible"; info_text.innerHTML=info_output document.getElementById('iframe_container').style.overflow='auto' document.getElementById('viewer').src=('Location_Files/')+(office3)+('.htm') var coordinates='$'+(office3) document.getElementById('compass_rose').style.top=('200') document.getElementById('compass_rose').style.left=('200') document.write(coordinates) } Similar TutorialsHow can I call a function in javascript like: Code: var x = string.Length ??? and not like: Code: var x = Length(string); (just for arguments sake) (Also please don't suggest JQuery, I'm doing this to learn!) Thanks! Hi all, I'm having trouble calling a function using a variable, from within a parent function. For instance: Code: function parent(){ function child(){ console.log("It worked") } var test = "child"; window[test](); } parent(); That results in "TypeError: 'undefined' is not a function (evaluating 'window[test]()')" Any thoughts? Is there a better way to do this? Cheers! How can I use a string to select a variable? E.g. Code: function load_menu( menu ) { //Availible menus var login = Array('Login','Register','Recovery'); var settings = Array('Profile','Account','something'); //Menu container ID's var cons = Array('menuitem1','menuitem2','menuitem3'); for ( var i = 0; i < cons.length; i++ ) { document.getElementById(cons[i]).innerHTML = menu[i] ; alert(menu[i]); } } "menu" is "login". menu[i] should reference the "login" variable and then put one of the values for the login variable array in its place. If you understand what I mean? So if menu is settings then the settings array would be used. Here is my code Code: function copy_grid(from,to) { var copy_from = from +"_24_product_cost"; // in this instance from = grid1_White var copy_to = to; var tmp_field = getElementById(copy_from).value; // ERROR: getElementById is not defined } document.product.grid1_White_24_product_cost.value // works as expected but I wanted this to be dynamic. Any ideas? Say I had a bunch of elements with id names "id1", "id2". "id3" etc. Then say I had a function that adds a click handler such that when you click these elements it gets the id name with this.attributes[1].nodeValue;. Then say I had a bunch of arrays with the same names as the ids var id1 = ["data", false, 45]; var id2 = ["otherdata", true, 15]; var id3 = ["otherotherdata", null, 65];. How would I set a variable "currentid" to the array with the corresponding name as the id name? I guess the underlying question is, how would I convert a string to a variable name?
Hi all, I am having a hard time on this, can you help pls? I have this code? $("select[name=objeto"+contcheck+"]").change(function(){ $("select[name=titulo"+contcheck+"]").html('<option value="0">Carregando...</option>'); where contcheck is a counter, so a number. It does not work at all, any help how to concatenate string and variable like this: objeto1 objeto2 objeto3 .... and so on. Thx. I am making a function where I need to know whether the input is a variable with a string value var foo = "bar"; Function(foo) or if it is a string, wherein it would be the name of an object object = { ThingsInside: function(){code} } Function('object') Code: function Function (object){ if(object is a string){ do this } else if (object is a variable with a string value){ do this } else{ do something else } Because they would be both passed by value, I'm not sure how or if there is a way to tell as they would both just appear as string inputs, right? I am just starting to learn javascript and was hoping someone might be able to answer my question: var months = "April Showers"; document.write('<p><img src="ad11.jpg"); document.write(" alt="); document.write(months); document.write(">"); document.write(months); How come when I display my output to a browser without the presence of the ad11.jpg file, only the first word (April) in the text string "April Showers" is dispayed where the .jpg file is suppose to be...but the last line displays the entire text string. Is this possible? Code: String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g, ""); } var myStr = " Hi what's up? "; myStr.trim(); alert("{" + myStr + "}"); //will result in the original string, not the trimmed version It's possible with numbers: Code: var myNum = 10; myNum++; alert(myNum); //11 Hi, I have a javascript program that currently takes the contents of a *.txt file and converts it to a string-type variable using a hidden iframe. For my pruposes it would be much nicer to read a *.csv file instead, however the method I'm using creates the "open/save file" pop-up window. All files are client-side and in the same folder on one computer; this is NOT server-side or internet-based. It is simply javascript used in an HTA file as programming code. The *.csv does not need to be edited, just read. I need to know how to convert the text of this *.csv file into a string variable. I have heard of using "xmlHTTPRequest (AJAX)" but am not familiar with how to use this. Any advice or code would be greatly appreciated. Hi All, Am new to Javascript but fluent in C++ and other languages, I have a query, i made a global variable to hold string information, can the value of this string be assigned to a form input field value? I mean in the 'default value sense' I am just wondering if there is any syntax to accomplish this. EG in my javascript i have the string nameVal; It is a global variable, i would like to know if it is poosible access its value for use in the line below shown with italics, or is it basically always a case of 'type the value in manually yourself' and then rely on events functions to update the field thereafter? Code: Name: <br><input type = "text" name = "nameField" size = "50" value = my string val here onChange ="this.value=storeVal(this.value, '0')"></input></br> I need to declare a variable (astString) and initialise it with a string of asterisks. astString needs to be the same length as another variable (otherString). otherString changes it value regularly so I think I need to use the .length property in some way but I can't see how! This should be simple but it's eluding me completely. Hey, I'm looking for a way to get a variable from a parent window in a child window. I'm aware that you can write the variable to a hidden field and then get the field's value in the child window, but I'm looking for a cleaner (all JS preferable) way of doing this. Something like this, but I believe this is an out-dated solution: Code: var str = window.opener.str; alert(str); Any solutions? Hello, I am trying to update an iframe when the user makes a selection from a select box. I don't have any trouble with changing the iframe, but when I try to send a varible as a quey string, the variable doesn't pass to the iframe properly. // this is in my header, and it is initiated from the onchange below. // it changes the page fine, but doesn't read the varible "num" // that was passed to the function <script language="javaScript"> <!-- function updatenotes (num) { document.getElementById("desc").src = 'iframe1.cfm?dec=num' } //--> </script> // this is the select box that initiates the function from onchange // for simplicities sake, I hard coded 3 options, but in reality, // these options are ever changing <select name="Description" onchange="updatenotes(this.options[selectedIndex].value)"> <option name="">Select a project</option> <option name="2" >project2</option> <option name="3" >project3</option> </select> Any help you can offer would me ver appreciated. thanks Hi, I am hoping I just need to be pointed in the right direction with this. I have Page1. When Page1 body onloads it uses Ajax to call PartA Within PartA I have a message board so members can write messages which will be sent to my database in PartA[1] and immediately posted for view on to PartA[2]. As I want to have my request to the server updating regularly I want to have PartA[2] on a timed loop to refresh - I do not need the content of PartA[1] to refresh. So the order of events would look like this: Page1 | onload call | v PartA / \ V V PartA[1] PartA[2] (loads once) (constantly refreshes) What I am not sure about is that I have <body> and <head> attributes in Page1 only. I have already used my body onload to call PartA (and can't use it to call PartA[2] before PartA has loaded anyway). I do not want the user to have to click a button or do anything to call up PartA[2]. So my question is how would I get PartA[2] to automatically load within PartA? I hope I have made this clear, but if I haven't let me know and I will try again. I have this function: Code: function SelectedRadio(form, name) { for(var i = 0; i < form.name.length; i++) { if(form.name[i].checked == true) { return form.name[i].value; } } return "Error!"; } Form is a form object, and name is a string of the name of the radio button. I thought it was trying to use the variable name as the name of a field. For example: Code: function OnFormSubmit(form) { SelectedRadio(form, "fruits") } Instead of form.fruits its using form.name. Basically I am trying to use the variable as a string. Is there anyway around this? Hey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response Hi, I have a programing problem that have been around for ages. I have search on google using several expressions and words and after hours of digging i'm still unable to do it. I would like to get a value from a HTML page hosted remotely with an inconstant value. Then define this value and name as a javascript variable and apply or show in my page. Thanks for all the help P.S. Is there any way to make a domain lookup in javascript? I mean a user enters a domain and the script converts to an ip and shows to the user. If not thanks, probably it can only be done in the server side... I don't know how I should do ? Quote: <html> <head> <script type="text/javascript"> function add(a,b){ y=a+b return y } var aaa = add(one,two) //one needs to get somehow get the value of yil, in this case 10 var one = function reas(){i=10;if(i<10){yil = 15}; else{yil = 10; yil = one;}; var two = 20; document.write(y) </script> </head> </html> also why doesn't this work? Quote: <html> <head> <script type="text/javascript"> function adder(a,b){ var k=a+b; return k } var hes=adder(5,kol); kol=40; alert(hes); </script> </head> </html> you cannot have variables in callback functions? |