JavaScript - Js String Creation From An Array
Hello,
1. I have the following script: function selectThis(id){ var locationstring = "process.php?value=" + id; ///// locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex"); locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex"); locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex"); locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex"); locationstring += "&in=" + $('#in_' + id).attr("selectedIndex"); document.location.href = locationstring; } With it I am getting a string with the value of the clicked button from an array and some other select field values that are listed in the same set of mysql results as the clicked button. BUTTON: <input name="button" type="button" onclick="selectThis('<? echo $rows['id']; ?>');" value=""> At the end of the script the page is redirected to in the script mentioned page with following string: process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6 (values are not real, so whatever the chosen value of the select field should apear in the link.) THIS WORKS FINE. 2. My problem is following: In order to be able to use var locationstring in other scripts as well I have removed var and have made it global. function selectThis(id){ locationstring = "process.php?value=" + id; ///// locationstring += "&bs=" + $('#bs_' + id).attr("selectedIndex"); locationstring += "&ad=" + $('#ad_' + id).attr("selectedIndex"); locationstring += "&ns=" + $('#ns_' + id).attr("selectedIndex"); locationstring += "&wt=" + $('#wt_' + id).attr("selectedIndex"); locationstring += "&in=" + $('#in_' + id).attr("selectedIndex"); } The problem is that later on when I use locationstring variable in other script I get only first variable in the string process.php?value=1 instead of process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6 3. Is it possible to make locationstring variable global and in a single line in order to be able to use the full string in other scripts or is there another solution. Thank in advance. Similar TutorialsI want to create an array of 1 - X (X could be 10, 20, 30 or any number) Then I want to randomly choose a number from 1 - X I now want to remove the random choice - and then do the same again twice Question 1: How do I create the array in the first place? Answer: var x=[0,1,2,3,4,5,6,7,8,9,10]; I'd much prefer it if I could do something like this: var x=[0...10]; This would create the same, except, I don't have to type out all the numbers! And if I needed to increase the number in the array, I could simply just change the last number I've just made the syntax up I was hoping something similar shorthand existed Or do I have to do this by creating a loop? Question 2: how do I pop an item from the middle or somewhere other than the beginning or end? I've googled and all I can find is how to remove the first and last items! The way I see it: - Get a solution for popping from the middle of an array I'm sure I'll find a solution to this if I looked - but is it optimal to be doing it this way? - Instead, maybe I could fill my array in a random order in the first place - and then just pop at the beginning or end?? Which is the best solution? Any code to start me off would be great Thanks OM I have an array: Code: var array_123=["test1","test2","test3"]; I have a string: Code: var array_string = "array_" + "123"; I would like to use the value of array_string (array_123) as an array to parse out the values of it. But this isn't working: Code: for (var x in array_string) { var test_ary = array_string[x]; } Any help? So i've written up the code to do a lightbox esque overlay image gallery. everything is working smooth and fine. The only concern is that I have the following: lets say that I have a page that has multiple galleries. Obviously I would have to make each literal array with the gallery data different names, ex: Code: var _gallery1 = { 'images': [ { 'id' : 1, 'src' : "" } ] } var _gallery2 = { 'images': [ { 'id' : 1, 'src' : "" } ] } or would it be easier to do it multi dimensional? Code: var gallery_data = { 'gallery1': [ 'images': [ { 'id' : 1, 'src' : "" } ] ] 'gallery2': [ 'images': [ { 'id' : 1, 'src' : "" } ] ] } regardless of which direction I use... my main question is this: I have two variables which are the following: Code: galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null; galArray = _gallery2.images; I'm unfamiliar with a way of doing something along the lines of this, and having it actually work: Code: galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null; galArray = galVariable.images; obviously "galVariable" would be undefined... as i'm trying to get "_gallery2.images" but trying to use "galVariable" to point to "_gallery2" I'm familiar with solving a problem like this in PHP, not so much javascript. is there a JS function that can be used to ensure that it's using whatever "galVariable" equals to, and then that array's content is? Or is there a different way to go on about this. thanks in advance. if anyone needs some more clarification. please feel free to ask, and I'll respond asap. I have var att=7,8,9 how can I convert these to an array so I can loop through the values? Hello everybody ! I'm searching for a javascript function that convert array to query string. Like this example : Code: var array = { a=1, b=2, c=3 }; to a=1&b=2&c=3 I'm using jQuery and the function .serialize() convert only html forms. Does someone know a function to do this ? Thanks ! Hey. I have a string and I can return say, the fourth letter with output[3], but i can't assign a value to it for some reason: I am trying to make hang man Code: <!DOCTYPE html> <html> <head> <script src="jquery.min.js"></script> <script> $(document).ready(function(){ var word, output, wordLength, stringLength, newLetter, letters; var i, n, k, health, wcheck; word = "word"; output = ""; letters = ""; wordLength = word.length; for(i = 0; i < wordLength; i++){ output += "_"; } $("#inputLetters").keyup(function(){ newLetter = $("#inputLetters").val(); $("#inputLetters").val(""); letters += newLetter; wcheck = false; for(i = 0; i < wordLength; i++){ if(word[i] == newLetter){ wcheck = true; output[i] = newLetter; } } if(wcheck = false){ health++; } $("#letters").html(output + "<br />" + letters); }); }); </script> </head> <body> <span id="letters"> </span> <br /> <br /> <input id="inputLetters" type="text" /> </body> </html> Hi, I am trying to search an array for a string and then if it is found alert it. I have found examples of how to iterate the array and how to use .IndexOf to return a true false statement as to whether the array includes the string, but i don't know what to do after that and how to display the string if its found. Any help appreciated.. Thanks Hello. I need help PLEASE ! I need to convert a string Quote: lalaw|lalaw1|lalaw2|lalaw3 in to Array The values I have in variable "tables" I want to create variable list which takes values from "tables" Than I want to split this, and put each value in to new array: I've started with: Code: var list = "tables"; var listArray = list.split("|"); for(i=0; i < listArray.length;i++) { //? how to put now values in to array? } Best Regards Leos Hi all, Forgive my ignorance, I'm quite new to Javascript... I'm looking to do two things: the first is to move elements of an array to the next index using a function. I have managed to do this like so: Code: var letters = ['h','i','j','k']; function moveElements(anArray){ var newArray = anArray; newArray.unshift(newArray[newArray.length-1]); newArray.pop(); } moveElements(letters); (this may or not be great, but it seemed to work) ... but I want the original array to reset to its original state if the function is called again (thus producing the same answer as opposed to shifting the elements again). Secondly, and this is the bit I'm really struggling with: I want to compare each letter of a string to elements within an array and display the indices. For instance: Code: var myArray = ['a', 'b', 'c', 'd', 'e', 'f']; var text = 'dead'; Hence I want the result '3,4,0,3'. I know the answer will use for and if statements but I can't wrap my head around it at all. Help would be appreciated, thank you. Hello All, I have a string array in a class file that contains 5 values, which I need to display on the jsp page. but when I send it to the javascript it prints [Ljava.lang.String;@104f8b8 . what do i do about it. I am a new to javascript. Is there a way to get my array data from the response object. Please help. Hi Here's what I want: A bracket/tournament creator on my website, so that users can start their own matches and just use my site as "scorekeeper". Here's what I've come up with so far: http://javascript.internet.com/misce...ournament.html provides a basic sketch of what I'm looking for. Can that script be changed so that multiple tournaments/matches can run at the same time. Is it also possible to only allow the participents of that specific tournament to submit the score? I'd really appreciate any help. I'll also be willing to acknowledge whatever help I get on my site, so that you can link to it, if someone wants to see your work. Thanks in advance! Hi, I know that it is possible to create map via javascript, which has some spots used like links. I have 3 images: background, house and man. Also 3 php variables: $man = 'yes'; $background = 'yes'; $house = 'no'; What I need is to make javascript map which would show background(yes) and man (yes). House must be invisible, because I have NO. The problem, that man must appear to 21px width 100px height and be active as a link. If you haven't understood what I want - ask. Now I will try to explain this with example. You have a map with countries. But map is like image and you can't do anything. If variable with country gets value "yes" - the country become active on the map and you can push on it. Thanks in advance... hi, I want to create a litebox with one or two inputs in it and submit button. These inputs would work in jQuerry to update information in main page.. I see many websites are using them, but i am not so good in javascript if you know any examples of simple litebox which could work with jQuerry, please inform me. All suggestions are welcome. Kind regards, Aurimas. Hi! I have encountered a problem with IE that I'm simply unable to solve. I have a form where the user can choose different things to input from a drop down list and depending on the choise, different kinds of textboxes of text areas etc. are loaded onto the page with JS. After the user has finished inputing text and submits the form, I read the input with PHP and process it further. Now, this works perfectly in firefox but IE doesn't seem to add the name properties to the elements (both textboxes and text areas) because PHP cannot find them and no info is printed from the input. The creation of the elements (adding them to the page...) works just fine, it's just getting the data from them that's the problem. Since it works in FF I know it's not a PHP problem. I've used the recommended .name to set the property (although I've also tried .setAttribute() etc), yet it still doesn't work. What can I do to solve this? My relevant JS code: Code: function addTextbox(idName, head) { var target = document.getElementById('addThings'); var newDiv = document.createElement("div"); newDiv.id = "container"; newDiv.name = "container"; newDiv.setAttribute("className", "intNew"); //IE newDiv.setAttribute("class", "intNew"); //FF var newTextbox = document.createElement("input"); newTextbox.type = "text"; newTextbox.id = idName; //-- newTextbox.name = idName; //Doesn't work in IE?.. newTextbox.setAttribute("className", "newWidth"); //IE newTextbox.setAttribute("class", "newWidth"); //FF var text = document.createTextNode(head + ":"); target.appendChild(newDiv); newDiv.appendChild(text); newDiv.innerHTML += "<br />"; newDiv.appendChild(newTextbox); newDiv.innerHTML += "<p />"; } (The text area function is the same, more or less) The PHP code, if anyone's interested: Code: if($_POST['createBtn']) { $head = $_POST['head']; //1 $intro = $_POST['intro']; //2 $question = $_POST['question']; //3 $answer = $_POST['answer']; //3 $image = $_POST['image']; //? $author = $_POST['author']; //5 $end = $_POST['end']; //4 //sammanfattning printHTMLTop(9); $today = date('Y-m-d'); $text = <<<END <div class="intContainer"> <div class="intHeadRow"><b>$head </b></div> <div class="stpdIEContainer"> <div class="intTextContainer"> <p /> $intro <p /> END; if($question != "" && answer != "") { foreach($question as $k) { $text .= $k . "<p />"; foreach($answer as $j) { $text .= $j . "<p />"; } } } $text .= <<<END <p /> $end <p /> <i>Skrivet av: $author den $today</i> </div> <!-- intTextContainer --> <div class="intImgContainer"> </div></div> <!-- stpdIEContainer --> </div> <!-- intContainer --> END; //Prints to new file (on server) $file = file_put_contents('interviews/interview01.html', $text); //Set name //Add to DB //print newly created file $page = file_get_contents('interviews/interview01.html'); echo $page; printHTMLBottom(); 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 I am working on a page where the user will select a location from a dynamically generated dropdown list. I was able to create the php multidimensional array (tested and working) from a MySql database using the users information at login, but I'm having problems converting it to a javascript multidimensional array. I need to be able to access variables that I can pass to a number of text fields within an html form. For instance, if a user belongs to a company with multiple addresses, I need to be able to let them select the address they need to prepopulate specific text fields. php array creation: Code: if ($row_locations) { while ($row_locations = mysql_fetch_assoc($locations)) { $mail[$row_locations['comp_id']]=array('mailto'=>$row_locations['mailto'], 'madd'=>$row_locations['madd'], 'madd2'=>$row_locations['madd2'], 'mcity'=>$row_locations['mcity'], 'mstate'=>$row_locations['mstate'], 'mzip'=>$row_locations['mzip'], 'billto'=>$row_locations['billto'], 'badd'=>$row_locations['badd'], 'badd2'=>$row_locations['badd2'], 'bcity'=>$row_locations['bcity'], 'bstate'=>$row_locations['bstate'], 'bzip'=>$row_locations['bzip']); } } javascript function - this should create the array and send variables to text fields. Code: function updateAddress() { var mail = $.parseJSON(<?php print json_encode(json_encode($mail)); ?>); { if (comp_id in mail) { document.getElementById('mailto').value=mail.comp_id.mailto.value; document.getElementById('madd').value=mail.comp_id.madd.value; document.getElementById('madd2').value=mail.comp_id.madd2.value; document.getElementById('mcity').value=mail.comp_id.mcity.value; document.getElementById('mstate').value=mail.comp_id.mstate.value; document.getElementById('mzip').value=mail.comp_id.mzip.value; } else { document.getElementById('mailto').value=''; document.getElementById('madd').value=''; document.getElementById('madd2').value=''; document.getElementById('mcity').value=''; document.getElementById('mstate').value=''; document.getElementById('mzip').value=''; } } } Where is this breaking? Thanks in advance. I can't seem to figure out how to accomplish this. In my website, I would like the user to input text into a single or multiple textbox(es) and then have the contents of the textbox(es) stored to either a variable or an array. Then I would like to have that variable/array compared to other arrays. Basically, the user is searching for items in a database. The user can search for as many or as little items as they want. Then the item(s) will be compared to multiple arrays to find out if what the user wants is in the database. So for example, let's say the user is searching for recipes that have all or part of these ingredients: chicken, broccoli, lemon, honey. So, there would have been a total of 4 textboxes...one for each ingredient. These ingredients are stored to an array..lets call it ingredient(). In the database of recipes, each recipe has its own array which includes the ingredients needed to make the recipe, we'll call them tag1(), tag2(), and tag3(). Now, I want the array, ingredient(), to be compared to each of the "tag" arrays to see if any of the "tag" arrays include exactly match the ingredient() tag in part or in whole. Is this possible? I have the following array that contain the people who are on off on certain time: Code: var all = [ ["1234", "Jim", "2011-10-23 00:00:00", "2011-10-25 07:00:00"], ["1235", "Jack", "2011-10-21 00:00:00", "2011-10-21 08:00:00"], ["1236", "Jane", "2011-10-11 00:00:00", "2011-10-11 00:30:00"], ["1237", "June", "2011-10-20 00:00:00", "2011-10-20 12:00:00"], ["1238", "Jill", "2011-10-14 00:00:00", "2011-10-14 11:00:00"], ["1239", "John", "2011-10-16 00:00:00", "2011-10-16 10:30:00"], ["1240", "Jacab", "2011-10-19 00:00:00", "2011-10-20 08:30:00"] ]; The above array, I wish to use javascript to insert into the FullCalendar (http://arshaw.com/fullcalendar/). I notice that the only way seems to be using the FullCalendar array style (or structure) as follows: Code: $('#calendar').fullCalendar({ events: [ { title : 'event1', start : '2010-01-01' }, { title : 'event2', start : '2010-01-05', end : '2010-01-07' }, { title : 'event3', start : '2010-01-09 12:30:00', allDay : false // will make the time show } ] }); So, the question is: How do I insert my array to match with the FullCalendar array? cause FullCalendar array had a different array structure from my array - and I don't think so that I can write in this way: Code: $('#calendar').fullCalendar({ events:all }); Appreciate any help provided. Hey guys, I'm hoping this is possible or that there is an easier way to do this. I'm having an issue with displaying data from one array that contains information about users in a table that is controlled by a different array. Is it possible to do this or is this use of arrays to display the data the wrong approach? The table is located on one webpage, I simply want to extract one piece of information that I have placed in the initial array as part of the login script that contains user information (for validation for login etc) and display it in a table on the new webpage that is opened as a result of successful validation of the user details. I'm completely stumped and after many attempts I just can't seem to get it to work. |