jquery - combining two variables to set a new variable to an already declared variable using javascript -
jquery - combining two variables to set a new variable to an already declared variable using javascript -
i trying set new variable same value declared variable combining 2 variables create name of original variable... may sound confusing, here's example:
// javascript document document.write (finalvar); $(document).ready(function() { var position_1 = $("#box_1").position(); var left_1 = position_1.left; var top_1 = position_1.top; var position_2 = $("#box_2").position(); var left_2 = position_2.left; var top_2 = position_2.top; var box; var boxlength; var boxnumber; var selected = 0; $("#box_1").click (function() { if (selected == 1) // if box selected run next { box = $(".selected").attr("id"); boxlength = box.length; boxnumber = box.charat(boxlength-1); // finds number of box alert(+boxnumber); if (box == "box_1") // if selected box mimimise box, remove selected class , set selected 0 { $("#box_1").animate({height:50,opacity:0.8,left:left_1,top:top_1,borderradius:4,mozborderradiu s:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function() { $(this).removeclass("selected"); }); selected = 0; } else { $(".selected").animate({height:50,opacity:0.8,left:left_+boxnumber,top:top_+boxnumber,borderradius:4,mozborderradius:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function() { $(".selected").removeclass("selected"); $("#box_1").animate({width:900,left:60,top:0,borderradius:10,mozborderradius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function() { $("#box_1").addclass("selected"); }); } );} } // end of function if box selected else // if no box selected run next { $("#box_1").animate({width:900,left:60,top:0,borderradius:10,mozborderradius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function() { $("#box_1").addclass("selected"); }); selected = 1; } }); $("#box_2").click (function() { if (selected == 1) // if box selected run next { box = $(".selected").attr("id"); boxlength = box.length; boxnumber = box.charat(boxlength-1); // finds number of box alert(+boxnumber); if (box == "box_2") // if selected box mimimise box, remove selected class , set selected 0 { $("#box_2").animate({height:50,opacity:0.8,left:left_2,top:top_2,borderradius:4,mozborderradius:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function() { $(this).removeclass("selected"); selected = 0; }); } else { $(".selected").animate({height:50,opacity:0.8,left:left_+boxnumber,top:top_+boxnumber,borderradius:4,mozborderradius:4},(60),"swing").animate({width:50},(60),"swing").animate({opacity:0.6},(150), function() { $(".selected").removeclass("selected"); $("#box_2").animate({width:900,left:60,top:0,borderradius:10,mozborderradius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function() { $("#box_2").addclass("selected"); }); } );} } // end of function if box selected else // if no box selected run next { $("#box_2").animate({width:900,left:60,top:0,borderradius:10,mozborderradius:10},(60),"swing").animate({height:500},(60),"swing").animate({opacity:1},(150), function() { $("#box_2").addclass("selected"); selected = 1; }); } }); }); i want 5 written document... there way of doing this? know not right way begin thinking doing this, using synonym trying do.
thanks help.
if these global variables, can this:
var position = 4; var = "posi"; var b = "tion"; document.write(window[a+b]); this works because global variables properties of window object , can reference properties of window object either window.position or window["position"]. since latter works, can build string "position" using string operations in illustration above.
i inquire why you're doing this? 1 mutual reason people inquire can access variables position1, position2, etc.... if that's case, improve reply utilize array can accessed index:
var positions = [1,4,67,99]; document.write(positions[3]); you can access array values via variable this:
var positions = [1,4,67,99]; var pos = 3; document.write(positions[pos]); or, iterate on entire array:
var positions = [1,4,67,99]; (var = 0; < positions.length; i++) { document.write(positions[i]); } if describe real problem you're trying solve, can recommend best way solve it. you're asking sounds wrong approach pretty much problem.
jquery variables operators arithmetic-expressions
Comments
Post a Comment