JavaScript - Concatenating Variable Names
Ok, below is an example of what I am trying to do (it's easier to show than try to explain).
Code: var abc1 = "this text "; var abc2 = "that text "; var abc3 = 483967; var xNum = 2; var myNum = "abc"+xNum; alert (myNum); // this gives the expected "abc2" concatCall(); function concatCall() { var newOne = myNum + abc3; //the output I want is "that text 483967" instead of "abc2483967" alert (newOne); } Now in the function, the variable is behaving as it should and I expected that, what I want though is what is commented. Any ideas on how that can be done? I know it has to do with the fact that once the var and string are concatenated, they become a string. What I want to know is the way to convert the string into a variable name that will produce the variable's data instead of the variable name. (for the curious, I have a <ul> that is passing the xNum var to my JS and currently have an else if statement to output the correct data - i'm looking for a way to shorten this code) Similar TutorialsHi guys, I have a small script that creates elements with createElement(); and assigns an id to it with a variable concatenated to the id, but when the function is ran again it seems like the previous div that was generated doesn't retain the previous id with the concatenated variable. This is my code: Code: <script> function effect(){ $('#btn-slide' + defectsnumber).click(function() { $('#table2' + defectsnumber).animate({ height: 'toggle' }, 2000); }); } </script> <script type='text/javascript' language='JavaScript'> defectsnumber = 0; function generatedefects() { if (defectsnumber > 9) { alert('You can not enter more than 10 defect percentages!'); } else { if (defectsnumber == 0) { testdiv2 = document.createElement('div'); testdiv2.id = 'testdiv'; } defectsnumber = defectsnumber + 1; testingdiv = document.createElement('div'); testingdiv.id='btn-slide' + defectsnumber; testingdiv.innerText = 'Click Me ' + defectsnumber; divTag2 = document.createElement('div'); divTag2.id='table2' + defectsnumber; divTag2.style.cssText = 'background: white; border: 1px solid black; width: 300px; height: 300px; margin-left: 34px; margin-top: 5px; float: left; display: yes;'; divTag2.appendChild(testingdiv); testdiv2.appendChild(divTag2); document.body.appendChild(testdiv2); } effect(); } </script> Does anyone know why that is happening? I have a form with several types of checkboxes, and I'm trying to check all but one of them to determine if any of those have been checked. I do not want to check 'all', but I want to see if any of the 'check_' or 'check2_' checkboxes have been checked. There are a variable number of checkboxes, so this is just an example with two of each. Set 1 and Set 2 will always have the same number of checkboxes as each other, though. I'm having trouble getting the function to do this - so any guidance would be appreciated. Code: <script language="JavaScript"> function checkForNone() { checked=false; for (i=0,n=selectForm2.elements.length;i<n;i++) { if (document.selectForm2.getElementById("check_"+String(i)).checked || document.selectForm2.getElementById("check2_"+String(i)).checked ) { checked=true; } } if (checked==true) { alert("OK!"); return true; } else { alert("Please select at least one checkbox."); return false; } } </script> PHP Code: echo " <form name='selectForm2' id='selectForm2'> Overall: <br> <input type='checkbox' name='all' value='' class='resultsAll' id='all'> Set 1: <br> <input type='checkbox' name='check_1' value='01' class='results1' id='check_1'><br> <input type='checkbox' name='check_2' value='02' class='results1' id='check_2'><br> <br> Set 2: <br> <input type='checkbox' name='check2_1' value='01' class='results2' id='check2_1' disabled='true'><br> <input type='checkbox' name='check2_2' value='02' class='results2' id='check2_2'> <br><br> Check for none selected <input type='button' onclick=\"checkForNone();\" /> </form> "; Find it really hard to understand these random variable names and what they are doing, can anyone explain what is going on exactly in the bolded parts. I have a rough idea but the 3 arrays confuse me somewhat im sure the random variable names are pointers and counts but id love to hear an expert explain them for sure thanks if you can help or are willing to take the time I need help troubleshooting javascript I am using to concatenate two fields in a form and populating a hidden field with them. Here's the javascript: <script type="text/javascript"> function combName() { var fname = document.forms[0].fname.value; var lname = document.forms[0].lname.value; document.forms[0].name.value = fname + " " lname; } </script> Here are the form fields: <input id="fname" name="fname" type="text"> <input id="lname" name="lname" type="text" onKeyUp="combName()"> <input name="name" type="hidden" id="name" size="20" maxlength="80"> <input name="Submit" type="submit" id="OnSubmit" value="OnSubmit"> What am I doing wrong? Thanks. Hello, I have several event handlers on my page; one of them doesn't work. I'll post them all here, indicating how they are executed: #1: Works [CODE]<script type="text/javascript"> function combName() { var fname = document.forms[0].fname.value; var lname = document.forms[0].lname.value; document.forms[0].name.value = fname + " " + lname; } </script>[CODE] NOTE: The "lname" field uses "onKeyUp" to make the script run. A hidden field named "name" is populated with the concatenation. #2: Works (this script is too long to post; it is a validation script and runs via the "onSubmit" in the form tag.) #3: Doesn't Work [CODE]<script type="text/javascript"> function combPrograms() { document.forms[0].00N70000002WNax.value = "1st Choice: " + document.forms[0].sf1.value + document.forms[0].alameda1.value + document.forms[0].sanmateo1.value + document.forms[0].marin.value + document.forms[0].cc1.value + "2nd Choice: " + document.forms[0].sf2.value + document.forms[0].alameda2.value + document.forms[0].sanmateo2.value + document.forms[0].marin2.value + document.forms[0].cc2.value; } </script>[CODE] Note: 1. Not all of the values for Choice 1 and Choice 2 are populated. 2. For that reason, one of the later, unrelated text fields in the form has "onKeyUp="combPrograms()" to run the event. 3. A hidden field named 00N70000002WNax is populated with the concatenation. Any help is much appreciated! Hello, I am using javascript to dynamically edit a form and have run into some problems. In this excerpt of code "x" is already defined, "num" is already defined, and "eval(q)" is just pointing to the proper field in the proper form. Code: eval(q).onkeydown=function onkeydown(event) { checkLength(event,x,num,5); }; Now the problem is, it is literally calling the function with "x" and "num" instead of their values. I tried "eval(x)" and "eval(num)" but that literally calls it with that whole statement as the parameter. How can I call this with the actual values inside of x and num? Thank you. Hi all, Apologies if this is a lame question, but I could not find an answer. Firstly, I am unable to post full HTML as it is way too long and I don't have a server to upload it to yet. I have a form that accepts a name and DOB and stores it in an array. Later in the form, a layer is triggered. Within this layer the previously enterred name(s) and DOB(s) are retrieved from the array and displayed on screen, with the addition of two extra input text fields. The code within the layer is as follows. As you can see I'm using the loop to add the additional fields, which in itself, does what I want, but may also be the root of my problem. Code: for (i=0;i<40;i++) { if (Array[i][0] != null) { lyrSrc += "<table style='width:600px;'cellspacing=0 cellpadding=0 border=0>" lyrSrc += "<tbody id='table1'>" lyrSrc += "<tr><td> <\/td><\/tr>" lyrSrc += "<tr><td style='width:220px;'>Name:<\/td><td> " + Array[i][0] + "<\/td><\/tr>" lyrSrc += "<tr><td style='width:220px;'>DOB:<\/td><td> " + Array[i][1] + "<\/td><\/tr>" lyrSrc += "<tr><td style='width:220px;'>Text 1:<\/td><td>$<input type=text name=\"txtone\" size=10><\/td><\/tr>" lyrSrc += "<tr><td style='width:220px;'>Text 2:<\/td><td> <select name=\"txttwo\" size=10><\/td><\/tr>" lyrSrc += "<tr><td> <\/td><\/tr>" lyrSrc += "<\/tbody>" lyrSrc += "<\/table>" } } The array is designed to store multiple names/dobs. If more than one name/dob is enterred, what I'm trying to find out is how to append the fieldnames of the two additional text fields so that the fieldnames are unique, as it returns an error if I try to call the field values if there is more than one name/dob in the array. I would appreciate any assistance or points,nudges,pushes in the right direction. I looked at trying to add the extra fields dynamically, but I couldn't get it to work, probably because of the layer(?). cheers. I have just bought a JS animated web template. I made changes to the file names on the splash page (index.html) and 2 landing pages www.keithmacstanton.com/dez Now everything is all messed up. Help
I have a function that capitalizes surnames including Scottish surnames, I can't remember where I found it but it has been working perfectly....until recently! The function will uppercase names that start with mc or mac, so mcdonald will become McDonald and macdonald will become MacDonald. The problem is if someone has the surname mack, it comes out as MacK. I think a good fix would be to tell the expression there needs to be at least 2 characters after mc or mac for it to assume it is Scottish but I have no idea how to do it. The code I have so far is Code: var temp = s.value.toLowerCase().replace( /\b((m)(a?c))?(\w)/g, function($1, $2, $3, $4, $5) { if($2){return $3.toUpperCase()+$4+$5.toUpperCase();} return $1.toUpperCase(); }); Can anyone help tweaking what I have please? I'm useless on regular expressions Hi everybody, I have a php file with an almost-standard html form. The not-so-standard part is that the name-attributes of the form elements are dynamically generated as such: PHP Code: <? for($i=0; $i<5; $i++) { ?> <input type="text" name="field_<? echo $i; ?>" /> <? } ?> Now I want to do some javascript validation on the fields, but I'm having a bit of trouble accessing the values of the fields. My current code is as follows: Code: var i = 0; var num = <? echo $step1_sag_faktura_num; ?>; for (i=0; i<num; i++){ window['file_' + i] = document.forms["nyisagform_step2"]["sag_fak_file_" + i].value; } But this doesn't seem to work. It gives no error message. It just validates true. Can somebody tell me a way to do this? I'm no good at javascript and the code above is pieced together by bit I found on the internet. Thanks in advance, deafdigit I have 2 questons. 1) Is there a way for the loop counter to loop while less then [a php variable]? 2) Is there a way to use a counter in the name of the form elements? Example, instead of "switchBox1" use: "switchBoxCounter" instead of "nameaddy1" use: "nameaddyCounter" Code: if(document.form1.switchBox1.checked) { document.form1.nameaddy1.style.backgroundColor = "#E5E5E5"; } else { document.form1.nameaddy1.style.backgroundColor = "#FFFFFF"; } Hello All, I have a form where I ask my users for a comma seperated web site list. I want to make sure this list only contains the host names of URLs without any white spaces Original list: site1.com, http://site2.com, http://www.site3.com,sub.site4.com Edited list: site1.com,site2.com,site3.com,sub.site4.com Currently I have the following to get the comma separated list Code: var list = document.getElementById( "list" ).value ; Can some one please help me with above task I highly appreciate your help or direction. Hello everybody. I am trying to make it so JavaScript changes my form's SELECT to what my PHP session equals. My method has always worked, except for in this instance. I have not been able to come up with an engineering solution to my problem, so turn to fellow coders for help. This collaboration of code is a stripped down version of what I am wanting to do: PHP Code: <?php $_SESSION['service'] = 'C'; ?> <FORM action="" method="post" name="form"> <DIV> <SELECT name="service" onchange="this.form.action = this.value;"> <OPTION value="a/">A</OPTION> <OPTION value="b/">B</OPTION> <OPTION value="c/">C</OPTION> <OPTION value="d/">D</OPTION> </SELECT> <INPUT type="submit" value="OK"> </DIV> </FORM> <SCRIPT type="text/javascript"> document.forms["form"]["service"].value = "<?php echo $_SESSION['service']; ?>"; </SCRIPT> I am not receiving a JavaScript error and I believe the problem is with clashing use of value between the SELECT element and JavaScript instruction, and that the value being that of the OPTION value attribute. The behaviour of this collaboration of code will work; note the differences: PHP Code: <?php $_SESSION['service'] = 'C'; ?> <FORM action="foobar.html" method="post" name="form"> <DIV> <SELECT name="service"> <OPTION>A</OPTION> <OPTION>B</OPTION> <OPTION>C</OPTION> <OPTION>D</OPTION> </SELECT> <INPUT type="submit" value="OK"> </DIV> </FORM> <SCRIPT type="text/javascript"> document.forms["form"]["service"].value = "<?php echo $_SESSION['service']; ?>"; </SCRIPT> Does anyone know a solution to my problem? Hi, I am new to this forum but I am in dire need of help. I am sorry if this isn't they way you normally do things. I read through some of the rules, but it is late and I am in a hurry. Any help would be greatly appreciated. I need the image file names in my array to be shown in the text box. I am not quite sure how to do this. I have been trying for some time but cannot figure it out. Here is my code. <head> <title>Asn4CStartup.htm by Todd Bowman</title> <style type="text/css" > h1, h2, h3 {font-family: 'arial black';} .controls { font-family: 'arial black'; font-size:10pt;} </style> <script type="text/javascript"> /* <![CDATA[ */ // Note: variables declared inside a function // using the var keyword are all local variables. It means that these // variables are only known inside the function. Other functions cannot see // the values. // On the other hand if a variable is declared outside any function, these are global // variables. The values of global variables are available in all other functions. // Other function can change the values of global variables. // Declare a global var n: var n=0; // we will use this global variable to keep track of exactly which element of the array // is being displayed now. When the user clicks Next button, we will add 1 to n and // then display the image file in the nth element of the array // Declare a global array that will contain the names of image files. var imgArray = new Array(); // The following function will be triggered on the onload event of the page. function fillArray() { // alert ("Hello on load"); imgArray[0]="Bus1.jpg"; imgArray[1]="Bus2.jpg"; imgArray[2]="Fam1.jpg"; imgArray[3]="Fam2.jpg"; imgArray[4]="Honey1.jpg"; imgArray[5]="Honey2.jpg"; imgArray[6]="Map1.png"; // for ( i in imgArray) // alert (imgArray[i]); } function showNext() // This function will be triggered on click of the Next button { // alert ("Hello showNext"); // alert(n); n = n+1; //alert (n); if (n > imgArray.length-1) { alert ('You are already on the last image'); n = n-1; } else document.getElementById('imgPic').src='images/'+imgArray[n]; } // end showNext() function showPrevious() // This function will be triggered on click of the Previous button { // alert("Hello Previous"); // alert(n); n = n-1; // alert (n); if (n < 0) { alert ('You are already at the first image'); n = n+1; } else document.getElementById('imgPic').src='images/'+imgArray[n]; } // end showPrevious() function showFirst() //This function will be triggered on click of the First button { // alert (n); if (n == 0) { alert ('You are already on the first image'); } else document.getElementById('imgPic').src='images/'+imgArray[0]; } function showLast() { // alert (n); if (n > imgArray.length-1) { alert ('You are already on the last image'); } else document.getElementById('imgPic').src='images/'+imgArray.length-1; } /* ]]> */ </script> </head> <body onload="fillArray();"> <div> <h1> Asn4CStartup.htm by Mesbah Ahmed </h1> <h2>Windsurf Image Navigation </h2> Name of the image file shown below:   <input type="text" id="imageName" size="20" readonly="readonly" /> <br/><br/> <img id="imgPic" src = "images/Bus1.jpg" alt="picture" width="500px" height="350px" /> <br/> <input type="button" value="Next" class="controls" onclick="showNext();" /> <input type="button" value="Previous" class="controls" onclick="showPrevious();" /> <input type="button" value="First" class="controls" onclick="showFirst();" /> <input type="button" value="Last" class="controls" onclick="showLast();" /> <br/> <p> <img src="http://www.w3.org/Icons/valid-xhtml11.png" alt="Valid XHTML 1.1!" height="31px" width="88px" /> <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue.png" alt="Valid CSS!" height="31px" width="88px" /> </p> </div> </body> </html> Hi, I am new to indesign javascript. I need to collect all the style information from an indesign file and i have to display as combo box and the user should seelct the style name. The selected style names i have to list in a seperate text box and when i select okay i have to search some text and replace in the document where this selected style name paragraphs only. This is my requirement. Can anybody help me to do this? Thanks in advance Anil Hi I've got about 40 check-boxes on my 1 form. These check-boxes all have names (obviously) but they are NOT all unique names. The number of UNIQUE names on my form is probably around 8 or 9 (the actual number is NOT important). Because some check-boxes use the same names as other. It's set up this way, please don't ask me to change it, I can't change it. What's the syntax/method for looping through all the names? I was thinking I should do maybe a "for" loop; Code: for (var i = 0; i < fieldName.length; i++) But I don't know how to hold a variable of the unique names to start with. Can someone help please? I'm trying to move a list of names from a textbox (jim;bill;sam;cathy to a listbox. My code so will move the name to listbox but all one line. I need the list the names in the listbox so that can click them one at a time. Please see my code. How can I modify it to fill the list one name per line? function addItem(){ var tb = $get('<%=txtWhoTo.ClientID%>'); var rightListbox = document.getElementById('<%=lstNDisplay.ClientID%>'); if(strText.length > 0){ var nlength = rightListbox.options.length; rightListbox.options[nlength] = new Option(strText, nlength, false, false); } } I don't want remove any names from the listbox till I click the "OK" button and overwrite what is in the textbox. 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? Hello I have a php page in which I declared a js variable... right at the top of the page... <script type="text/javascript"> var tester = 0; </script> Later in the page I test this variable... <script type="text/javascript"> if (tester==1){ do some stuff!! } </script> But I keep getting an error variable _tester undefined Am I missing something obvious... I am new to js... but this seems really straightforward |