javascript - Use a var from mousedown() in the mouseup() event -


so looked @ my previous question, im building chess board in complete js , jquery (or @ least).

so pieces effectivly restricted in amount of squares allowed move need know position. (starting , ending position)

i wrote code below log starting row (integer) , starting column (integer) , on both mousedown() , mouseup()

var piece;         $('div').mousedown(function(e) {             e.preventdefault();             var selectedrow = this.getattribute("data-row");             var selectedcolumn = this.getattribute("data-column");             console.log(selectedrow, selectedcolumn);             piece = $(this).find('.pawn');         })         .mouseup(function() {             var selectedrow = this.getattribute("data-row");             var selectedcolumn = this.getattribute("data-column");             console.log(selectedrow, selectedcolumn);             if (selectedrow === selectedrow++ || selectedcolumn === selectedcolumn++){                 console.log('true :d'); //wont true because both selectedrow's same value             }             $(this).append(piece);         }); 

for far can see cant compare both values since both logs in different events. (please keep in mind im new both languages , im still learning).

my question if possible collect both values (starting , ending) , being able compare them each other.

the easiest approach make selectedrow_down , selectedcolumn_down selectedrow_up , selectedcolumn_up in global scope.

var selectedrow_down; var selectedcolumn_down; var selectedrow_up; var selectedcolumn_up;  $('div').mousedown(function(e) {     e.preventdefault();     var selectedrow_down = this.getattribute("data-row");     var selectedcolumn_down = this.getattribute("data-column");     piece = $(this).find('.pawn'); }) .mouseup(function() {     var selectedrow_up = this.getattribute("data-row");     var selectedcolumn_up = this.getattribute("data-column");     console.log(selectedrow_up, selectedcolumn_up);     $(this).append(piece); }); 

then refer value mouse event

also make global 2 dimensional array first keep track of chess pieces, see thread how create 2d arrays how can create 2 dimensional array in javascript?


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -