SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: weird variable scoping problem
-
May 26, 2008, 07:14 #1
- Join Date
- May 2008
- Posts
- 1
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
weird variable scoping problem
Hi
Have been programming an javascript for a number of years now, and just started to get involved with jQuery, and I've stumbled across a problem which has really got me stumped.
Code:function calc_calculate () { // empty all quotation areas $("#quotation-aggregates").empty(); $("#quotation-stones").empty() $("#quotation-total-discount").empty() $("#quotation-total-collect").empty() extras = calc_get_extras (); aggstotal = 0; agglist = $("<ul />"); if(extras['cement']['retail']) { aggstotal+=extras['cement']['retail']; agglist.append($("<li />").append("Bags of cement: "+extras['cement']['bags']+" (£"+extras['cement']['retail']+")")); } if(extras['sharpSand']['retail']) { aggstotal+=extras['sharpSand']['retail']; aggstotal+=extras['buildingSand']['retail']; agglist.append($("<li />").append("Bags of sharp sand: "+extras['sharpSand']['bags']+" (£"+extras['sharpSand']['retail']+")")); agglist.append($("<li />").append("Bags of building sand: "+extras['buildingSand']['bags']+" (£"+extras['buildingSand']['retail']+")")); } stoneslist = $("<ul />"); var stonestotal=0; $.post("/legacy/costCalculatorResults.php", { colour: $("#calc-colour input:checked").attr("value"), patio_size: $("#calc-size-input").attr("value"), pattern: $("#calc-pattern input:checked").attr("value"), paving_variety: $("#calc-variety-select").val()}, function(data){ stones = data.split ("\n"); for(i=0;i<stones.length;i++) { if (stones[i].length) { details = stones[i].split(","); stoneslist.append($("<li />").append(details[0] + ": " + details[1] + " (£"+details[2]+")")); stonestotal+=Number(details[2]); } } }); stonestotal=(Math.round(100*stonestotal)/100); // alert (stonestotal) $("#quotation-stones").append(stoneslist); $("#quotation-aggregates").append(agglist); $("#quotation-total").append(stonestotal); }
Any ideas on why this might be happening? Seems very weird to me that the scope of the variable changes after it gets used in the alert function.
Any suggestions of how else to get this variable out of the anonymous function would also be useful!
-
May 26, 2008, 07:48 #2
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
divide
Hi slightlymore, welcome to the forums,
I seeCode:// empty all quotation areas $("#quotation-aggregates").empty(); $("#quotation-stones").empty() $("#quotation-total-discount").empty() $("#quotation-total-collect").empty()
Code:$("#quotation-stones").append(stoneslist); $("#quotation-aggregates").append(agglist); $("#quotation-total").append(stonestotal);
If stonestotal is 0, can 0 be divided by 100 without breaking the code?Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
May 26, 2008, 08:57 #3
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
UNLESS i uncomment out the alert 4 lines from the bottom, in which case the alert says 0, then the correct value is appended to the document
Bookmarks