jQuery - Define variables based on array values -
i m not sure if proper way of defining variables based on array values.
the getparameterbyname() function returns url parameters
var t = gettab(); var paramarray = { 'year' : 'y', 'condition' : 'c', 'sort' : 's' }; /*below vars defined $.each var y = getparameterbyname('y'); var c = getparameterbyname('c'); var s = getparameterbyname('s'); */ $.each(paramarray, function(key, value) { //define y,c,s variables var value = getparameterbyname(value); console.log(value);//logs blank (nothing) if(value != "") { $("#sort-filter-number").append('<button id="'+value+'" class="refresh refresh-'+key+' btn btn-info btn-sm">'+value+' <i class="fa fa-times"></i></button>'); } }); tosort(y,c,s); //line 55 error : uncaught referenceerror: l not defined
you can define variables on window
object make global
, can accessed anywhere.
demo:
var paramarray = { 'year': 'y', 'condition': 'c', 'sort': 's' }; $.each(paramarray, function(key, value) { //define y,c,s variables window[value] = key + ' => ' + value; // ^^^^^^^^^^ document.write(value + '<br />'); //logs blank (nothing) if (value != "") { $("#sort-filter-number").append('<button id="' + value + '" class="refresh refresh-' + key + ' btn btn-info btn-sm">' + value + ' <i class="fa fa-times"></i></button>'); } }); document.write(y + '<br />' + c + '<br />' + s); // no error here
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
Comments
Post a Comment