I know that using for in loop to iterate over array elements is a bad idea but I have a situation where it is tempting me to use it. I have a two dimensional sparse array say "arr" which will have length to be some 50 but will have some elements like arr[1], arr[10], arr[23] at random locations. Each of these elements is again an array (since it is two dimensional) say arr[1][30], arr[1][24] & so on for arr[10 & 23] also. Currently I've coded it like this:
for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr[i].length; j++) { if (arr[i] && arr[i][j]) { // do stuff with arr[i][j] } } }
but i am tempted to do following
for (var i in arr) { if (!arr.hasOwnProperty(i)) { continue; } for (var j in arr[i]) { if (arr[i].hasOwnProperty(j)) { // do stuff with arr[i][j] } } }
In this program i attempting to iterate through an array using the charAt () function and then store these element positions in a new array called elementPositions.
The user selects a flightDestination from the flightDestinations array which is stored elsewhere, and what my program needs to do, is find all the array elements positions where the same flight destination name occurs in my array.
I then need to store these positions so i can use them elsewhere, and i'm pretty sure i need to use charAt() but i think my syntax may be wrong - contained in the for loop.
var yourDestination = readTheDestination(); var title = "<B>" + "You asked about Flights to " + "<B>" + "<BR>"; var flightInfo; var solution = "";
I'm trying to figure out how to do this via Javascript.
Lets say I have an array like so:
var myArray = [11, 33, 44, 23, 32, 43]; var finalArray = new Array();
And I have the following variables
var lastNumber = 33; var getNext = 2;
I want to use the lastNumber for a starting point in the array and then based on getNext, grab the next x numbers that getNext specifies and shove into finalArray.
That case moves forward in the array.
Second case would be the opposite, get the previous x numbers based on getPrevious in the array. So I'd start at lastNumber and then grab x numbers before it based on getPrevious.
var lastNumber = 33; var getPrevious = 2;
I'm not sure if you can traverse backwards in JavaScript. But more importantly I'm not sure how to point to lets say 33. How can I do myArray[1] essentially based on I'm pointing to 33 and then how can I grab the next x in myArray ?
However, this just fails. It seems like its getting stuck on the serializeArray aka an array of objects. I've gone the for loop route and it worked fine up until I started doing some ajax stuff...the variable binding behavior was all over the place and unusable.
I'm writing a base class for a project and one of the class's prototypes encapsulates cross browser event listening. One point of confusion is that removeEventListener requires that you know the handler. I don't have this info because I'm creating anonymous functions, so how do I do it?Here's the code:
I have a problem with understanding jQuery. In my case I have this JS file with following content (see below). This is an anonymous function, isn't it? The problem is this line:
I have been struggling with a form wizard all day. I'm using jquery stepy (form wizard) along with validation plugin. To cut a long story short, my first step is to get MySQL connection from form controls details. On submit ('next' button) a check is made on an empty hidden control ('hid').
So, if the function checkDBData passes, false should be returned, so that the form can progress to the next step. If the connection details fail, true is returned so that an error msg is posted.
Here's the checkDBData function: function checkDBData(){ var host = $('#mysql_host').val(); var username = $('#username').val(); var password = $('#password').val(); var dbname = $('#dbname').val();
The return values don't find their way back to the rules. However, if I hard code false and true to the function... function checkDBData(){ var host = $('#mysql_host').val(); var username = $('#username').val(); var password = $('#password').val(); var dbname = $('#dbname').val();
$.post("install/sqlcheck.php", {"host": host,"username": username, "password": password, "dbname": dbname}, function(msg){ if(msg.required == false){ return false; }else{ return true; }}, "json" ); return false; //or return true for testing purposes } This works. I assume it's due to the asynchronous nature of the ajax call.
I have the code this way in order to consolidate it since I would prefer to do that instead of checking the selected ID and manually checking against all possible IDs (which works flawlessly, it just takes up about 5x the lines and is not modular). What I have above also works, but there is one fatal flaw:
It seems like the anonymous function that is the onclick for each unselected element becomes "return changeTo(tab + '_id')", but I don't want it to be that. I want the argument to actually be what tab is instead of the variable.
What ends up happening is that after changeTo() is called for the first time, any element you click will result in the last element being the selected one, as if it's using the final value of tab as its return value.
This doesn't make any sense, though, since tab is local, and deleting it before the function exists doesn't work. Deleting elem at the end of the for loop doesn't work. I honestly don't understand what's going on or why it doesn't set the new onclick value correctly.
Basically I just want changeTo(tab + '_id'); to turn into changeTo('MYID_id'); instead, but it simply doesn't do that and I can't figure out a way how.
I have several functions with code along the lines of:
var xmlDoc = requestXML("ajax.asp?SP=SelectRelatedTags&tag=" + array[i]);
The requestXML() function includes the code:
var xmlDoc = null; http_request.onreadystatechange = function() { if (http_request.readyState == 4) { if (http_request.status == 200) { xmlDoc = http_request.responseXML; } else { alert('There was a problem with the request.' + http_request.status); }}}; http_request.open('GET', url, true); http_request.send(null); return xmlDoc;
However, the last line (the return) executes before the readyState reaches 4. How do I return the xmlDoc to the functions only once the xmlDoc has been assigned? I tried putting the return statement in a while loop with the condition that the readyState must = 4 - this worked, but makes the browser popup a message saying the script is slowing down the system.
I did search the forums but couldn't seem to find anything on this specifically. I basically need to pass a key event and a 'name' to nameCheck() after 3 seconds. This works fine in Firefox but Internet Explorer gives the error: Member not found. I'm more of a PHP guy than a JS one
<input type="text" onkeyup="nameCheckTimer(this.value, event)" value="" /> function nameCheckTimer(name, evt) { setTimeout(function(){return nameCheck(name,evt)}, 3000); } function nameCheck(name, evt) { //need name and the key event to be available here. I have code to handle the key codes which works fine }
I'm working with nested functions and trying to pass a 'this' value to an anonymous being used in an assignment for an event listener.So, this should plop a button inside our DIV and when clicked I'd like it to run the alert-ding; unfortunately it seems to want to run the function as defined under the buttons object which doesn't work out too well.
Normally, if you want to bind encapsulated event listeners to an object you have to test for support and then call the same function in different ways, for example:
But there's code repetition there - okay not very much, because it's just a function call ... but what if you wanted to use an anonymous function ..? Well you can't - the code repetition would be unacceptible.
Except that I've though of a way :) It's really obvious actually .. but I'm posting this in the hope that others will go "wow, that's blindingly useful" as I did when I thought of it :thumbsup:
Here it is - it takes advantage of square-bracket notation to use a string reference to the supported method:
I am loading a Html table through jquery ajax. In this table each row has a check box . when i click the check box i want to get all the details in the row. but i could not iterate through the table.
when i use $("#civilBillTable1 tr[class="+row+"]" ).each(function(){}); works perfectly, but the table is not loaded through jquery ajax
if i use below code, it will not work
$('input.chkAddItem').live('click',function(){ var itemId,description,unit,rate; $("#civilBillTable1").live()('each',function(){ // the table is loading through Ajax alert("h");
I have a problem in Javascript that I really cant figure out, I've tried for days! here it goes: I need to iterate through a form and get all the elements that has a class like "required" and then, save this elements id, because I have label elements that has a for="" tag with the same as the id for the input/dropdown in the form. To all these labels I need to apply a "*" in the end of the label. And all this in an onload function.
I have tried everything I can come up with but now I'm close to giving up so now I need help, how can I get these values and do this?
I think the last thing people seem to learn about in JavaScript when they're not coming from other programming languages is variable scope. Some even get all the way into AJAX without having learned about scope, and this is a time when it's really needed. Although the scope of JavaScript variables is non-complex by nature, it's something we should all get a full understanding for before we move too far. Section 1: What is "scope"? Section 2: The "var" keyword Section 3: The "this" keyword Section 4: Closures or "Anonymous functions and self-invoking closures
I'm trying to iterate through all the dropdownboxes on a page without much success! What I'd like to do is store the selected value for each of the dropdowns, separated by commas in a variable when a button is clicked. Each of the dropdown boxes 'id' begins with the string 'attrib-' which is how I'm trying to identify them.
I've managed to hide all the dropdowns with this code:
I have a form on which I've created 5 checkbox groups. Each of these groups consist of multiple checkboxes. The first group - it's id equals "MASTER" - contains 4 checkboxes. These checkboxes are labelled (that is, their actual label tags are) "A", "B", "C", "D". My vision is to have a user check, say, "A" in MASTER and this causes the "A" checkbox group to appear below the MASTER group. If a user then checks "B" in MASTER, then the "B" group appears below the "A" group. If the user then unchecks "A", then the "A" group disappears. And similarly for each of the four "subordinate" groups. My plan of attack was to iterate over the MASTER checkbox group, and for each checked box, somehow populate an array of their associated label tags. I would do the same for all of the checkboxes in MASTER that are not checked, placing their label tags in another array. I would then show and hide the subordinate checkbox groups based on these label tags. The subordinate groups have label tags equal to these captured label tags in these two arrays. All of this action would be triggered via a "click" action on the tag in which the MASTER group resides. I've tried all manner of techniques, including "$.each" and ".map" to populate arrays. I just can't get my mind around the needed syntax.
<div class="rounded red"> <p>Integer egestas neque vitae dui ultricies vel venenatis mi varius! </p> </div>
[code]....
This works,in that it at least added the new class whereas the other solutions didn't. Unfortunately it counts EVERY div, not just the ones with the class .client, I can't set my array of desired classes, and I have no idea how to make it restart the count after it hits #4.