Populating a JavaScript array with variables
Old JsFiddle
I'm attempting to create an array that looks like this
//mapping array
var mapping = [
[ "[POSITION]" , $( ".position" ).val() ],
[ "[NAME]" , $( ".name" ).val() ],
[ "[TOWN/DISTRICT]" , $( ".town" ).val() ],
[ "[REGION]" , $( ".region" ).val() ],
[ "[COUNTRY]" , $( ".country" ).val() ],
[ "[HOG/HOS]" , $( ".hoghos" ).val() ],
[ "[TOURIST ATTRACTION]" , $( ".tourist" ).val() ],
[ "[SECTOR1]" , $( ".sector1" ).val() ],
[ "[SECTOR2]" , $( ".sector2" ).val() ],
[ "[SECTOR3]" , $( ".sector3" ).val() ],
[ "[PITCHER]" , $( ".pitcher" ).val() ]
];
out of a variable list here
//vars
var ie1 = "[POSITION]";
var ie2 = "[NAME]";
var ie3 = "[TOWN/DISTRICT]";
var ie4 = "[REGION]";
var ie5 = "[COUNTRY]";
var ie6 = "[HOG/HOS]";
var ie7 = "[TOURIST ATTRACTION]";
var ie8 = "[SECTOR1]";
var ie9 = "[SECTOR2]";
var ie10 = "[SECTOR3]";
var ie11 = "[PITCHER]";
I've been trying to push them in using this code
var mapping = [];
mapping.push({ ie1 : $( ".position" ).val() , ie2 : $( ".name" ).val() , ie3 : $( ".town" ).val() , ie4 : $( ".region" ).val() , ie5 : $( ".country" ).val() , ie6 : $( ".hoghos" ).val() , ie7 : $( ".tourist" ).val() , ie8 : $( ".sector1" ).val() , ie9 : $( ".sector2" ).val() , ie10 : $( ".sector3" ).val() , ie11 : $( ".pitcher" ).val() });
which will then update the text on a page using this
$(".CYB").html(function(index,text){
for (var i = 0; i < mapping.length; i++) {
text = text.split(mapping[i][0]).join(mapping[i][1]);
}
return text;
});
after this, the variables are updated with new values so that a page refresh isn't necessary
//new vars
var ie1 = $( ".position" ).val();
var ie2 = $( ".name" ).val();
var ie3 = $( ".town" ).val();
var ie4 = $( ".region" ).val();
var ie5 = $( ".country" ).val();
var ie6 = $( ".hoghos" ).val();
var ie7 = $( ".tourist" ).val();
var ie8 = $( ".sector1" ).val();
var ie9 = $( ".sector2" ).val();
var ie10 = $( ".sector3" ).val();
var ie11 = $( ".pitcher" ).val();
when I throw in an alert it's easy to see where it's going wrong as
//alert with array
alert(JSON.stringify(mapping));
outputs
[{ "ie1":"","ie2":"","ie3":"","ie4":"","ie5":"","ie6":"","ie7":"","ie8":"","ie9":"","ie10":"","ie11":"" }]
It's treating the variables as text rather than loading their values but I'm not sure how to fix this.
So in summary, I'm trying to reach a stage where I can input data into text boxes and have it update content on a page. If I want to make changes I wish to be able to do that without a page refresh by saving the variables I entered and having them loaded into an array.
I'd be grateful for any help.
Old JsFiddle
Edit:
I updated it slightly and this is now working more correctly... However I need to take the first variables out of the on click event and they aren't loading when I do. I would appreciate help in finding a solution to this problem.
For posterity: I changed
mapping.push({ ie1 : $( ".position" ).val() , ie2 : $( ".name" ).val() , ie3 : $( ".town" ).val() , ie4 : $( ".region" ).val() , ie5 : $( ".country" ).val() , ie6 : $( ".hoghos" ).val() , ie7 : $( ".tourist" ).val() , ie8 : $( ".sector1" ).val() , ie9 : $( ".sector2" ).val() , ie10 : $( ".sector3" ).val() , ie11 : $( ".pitcher" ).val() });
to
mapping.push([ ie1 , $( ".position" ).val() ],[ ie2 , $( ".name" ).val() ],[ ie3 , $( ".town" ).val() ],[ ie4 , $( ".region" ).val() ],[ ie5 , $( ".country" ).val() ],[ ie6 , $( ".hoghos" ).val() ],[ ie7 , $( ".tourist" ).val() ],[ ie8 , $( ".sector1" ).val() ],[ ie9 , $( ".sector2" ).val() ],[ ie10 , $( ".sector3" ).val() ],[ ie11 , $( ".pitcher" ).val() ]);