Friday, 15 January 2010

jquery - Should I use a switch statement to associate CSS IDs with JavaScript variables? -



jquery - Should I use a switch statement to associate CSS IDs with JavaScript variables? -

function getvariablefromid(id) { switch (id) { case "gta1": homecoming gta1; case "gta2": homecoming gta2; case "gta3": homecoming gta3; ... case "gtj0": homecoming gtj0; } }

so have switch statement in code takes #id of div tag using .attr , associates javascript variable. each numeric code represents 1 square on chessboard-like grid. there 100 squares. there easier or more compact method performing calculation this, or simplest way?

use array instead of 100 variable , hundred conditions in switch. array array of strings/numbers or objects. declare array number of elements want , number id utilize index of array.

arrgta = ["somevalue1", "somevalue2"]; //could array of strings/numbers or objects. function getvariablefromid(id) { idx = parseint(id.replace("gta", "")); homecoming arrgta[idx-1]; }

javascript jquery html css

No comments:

Post a Comment