Declaring/Initializing Variables Inside Or Outside A Loop?
Apr 30, 2007
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.
What's a better practice?
Declaring and initializing variables inside a loop routine, like this:
for(var i=0; i<list; i++)
{
var name = list[i];
}
or outside a loop routine, like this:
var i;
var name;
for (i=0; i<list; i++)
{
name = list[i];
}
or are both the same....
As a programmer, i always try to practice good programming. I always
thought that by declaring and initializing the variable inside the
loop, i was creating a new memory space every time instead of just 1
time...
This is probably a really simple question. Is it possible to initialize x amount of variables. Like someone enters 10 into an input box and it makes 10 variables called variable1, variable2, variable3, etc.How would you name them?variable + num = 0; ?
I run a carousel plugin which has initializing variables on my index page, which looks like:
Now what i need is an onclick event which tells the script to update the variable 'start' to '500' and then re-run the script without a complete page reloading....
You can use JQuery's data method (e.g. $('#MyID').data('MyVar', 'MyValue');) to easily store/retrieve information.
However, I was wondering if it is possible for my PHP generated page to add HTML tags that initialize the JQuery's data method variables. Regularly, I want server side variables to be accessible client side. It would be very helpful if I could initialize data method variables during PHP page generation.
If this is not possible, are there any suggestions on how to efficiently communicate server side variables to client side?
I'm trying to save the input and pass it into the URLs that are the option values. So Artist ID being 111 would take you to url..../<WHATEVER OPTION YOU CHOSE FROM DROP DOWN>/111 What am I doing wrong/What am I not doing?
I'm looking to send a loop variable (i) to a function inside the loop, but I can't seem to get it to use the value I want, it keeps making it a reference of i and therefore the function is always called using the last value of i rather than the one it was set with.
So if i have 5 Tabs then Tab 1, when clicked, should call DefaultTabClick(0) and so on rather than always using 4 for any of the tabs.
What about if I had something like this where a for loop creates all the variables for you...rather than having to write them out on individual lines like so:
for(i = 0; i < myForm.elements.length; i++) { tempControl = myForm[i]; var e = tempControl.name; var [e] = document.myForm[e].value; }
I'm currently trying to produce an HTML table from an XML feed, using jQuery. And this works great! With an "each" loop, there now is a nice table on my screen, displaying the right information from the feed.
I would like for each new row of this table to have a separate color. For example: row 1 - blue row 2 - green row 3 - blue again row 4 - green again ... and so on...
What I've tried, is putting something like this inside the loop: var x=0 (this line should probably be outside of the loop, but that doesn't seem to work) if (x=0) code 1... x=x+1 else code 2... x=x-1 But this doesn't work.
I have a theoretical question, I can't really post my code. But I have four results from reads to a database using php. I then read from an xml file using javascript using a loop. I am basically creating a chart of four variables with several different values for each variable. Some of the values for each variables come from the database using php, and some come straight off the XML file parsing done by the javascript. I initially had the php fetch/extract inside of the javascript loop, but it only assigned the first variables values to all the other variables. I discovered (after hours of googling) the difference between client side and server side code. So, after all this typing, my question is where should I look for tutorials on using Ajax to be able to use php code inside of a javascript loop? (If at all possible)
I know that each of the objects in the array have data because I tried to run each one of the lines above separately without calling the other two and it worked fine.
I've attached a cut-down version of a script I am working on. It's a pretty simple button with a function attached, which creates a random number and compares it with a preset value.I can't seem to get the function to read the variables
we have a list of names, generated by parsing an array JS source file.The site and the main page is ASP, linked to a Database.Is there a way to store the JS-generated text, inside the Database?To be more clear:Code:
<!-- excerpt from mypage.asp --> <script type="text/javascript"> // VariableNames are an array result, but here we list a simple example:
I have a simple HTML page with one DIV element with the id "rotator". Inside that, JavaScript is supposed to create boxes that react to mouseovers.
The weird thing is: The whole script works, but ONLY on the LAST box I create, no matter what I do. I can manually add the mouseover code to any one of these boxes, but it will only take on the last one. I can have JavaScript tell me the mouseover status of each box, and they all tell me they have code assigned correctly - but again only the last one works...
Here's the relevant code (yes, highly abbreviated, but it's the part that fails on me):
var maxBoxes = 10; function initSite() { var rotator = document.getElementById("rotator"); rotator.innerHTML = "";
[Code]....
initSite is called in the body onload. All kinds of other animation parts are implemented that work fine, just this mouseover won't work. I have tried re-writing it in multiple different ways, including "xyz.onmouseover = myMouseCode" and then defining the function separately later - still no dice.
So, the code creates 10 boxes (0-9) and 10 boxes that are on top of them to create a form of shadow effect depending on the position of the original boxes. Since the "myDark" boxes are on top of the "myBox" boxes, I apply the onmouseover onto the "myDark" boxes, but it only works on "myDark9" and no other box. They are all created the same way, the mouseover assigned the same way...
I'd like to ask how can i ask for a function inside a for loop , if i remove the loop the code works fine but i need it for 10 rows .here is the code...
I am trying to understand the order of operations of a for loop.
for (count=1;count<3;++count) { document.write(count + ". nice rack.") }
when this runs it prints:
1. nice rack. 2. nice rack.
I dont understand how it prints 1. The way I see it it sees the for looks inside the parenthesis, assigns count a value of 1, goes to the comparison sees that it is less than 3, then gets incremented by 1 at the next station, and then finally goes in the curly brackets and the string is written. So if it starts with 1, is less than 3, and is increased by 1. before it starts the loop, then the first sentence would read:
I am calling a program using $.getJSON() inside of a for loop. The problem is that the second callback doesn't wait for the first program to finish before it executes. Is it possible to ensure that the second iteration of the loop doesn't happen until the first one is finished?
$('#dialog-form').dialog({ buttons: { "Import": function() { var ids = $("#itemsGrid").jqGrid('getGridParam','selarrrow');
Trying to add a new property to a non-jQuery object from inside each loop doesn't appear to work: var arguments=new Object(); arguments['ticket']=ticket; arguments['email']=email; arguments['module']=module; arguments['epoch']=new Date().getTime();
var total=0; $('select.category').each(function(){ var name=$(this).attr('id'); var option=$(this).find('option:selected').val(); total+=parseInt(option); arguments[name]=option; });
The 'total' variable works as expected. The the 'arguments' variable doesn't. Almost like a variable scope issue or something. If I put some extra debug code to print out all the property/value pairs, outside each loop I only see original four, and inside each loop only the new one just set and none of the original four.?
My function: function swap_content(id1,id2) { var tmp = document.getElementById(id1).name; var theval = document.getElementsByName('primary_propertyp_id')[0].value; document.getElementById(id1).name =
[Code]...
This is running through a PHP loop so it's making multiple divs and links for the secondaries. I am wanting to be able to swap out any of them to make them 'primary' this works for the first click, but after the first click it makes every div id and input name the same as the first that was clicked. It's also not working AT all if i click on the bottom link first, then a link above it. Top-down works, bottom-up doesn't..