Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  79] [ 4]  / answers: 1 / hits: 16883  / 8 Years ago, mon, july 4, 2016, 12:00:00

I am trying to passe an array from ejs to JavaScript. I can get to the values inside ejs but not from JavaScript. all the time i get undefined because the contents of the variable test is a string is not an array.



<script>

var test = '<%- level_tab %>';
alert(test);

function level(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = ;
if(s1.value == level_0){
var optionArray = test;
}
else if(s1.value == level_1){
var optionArray = [test|test01, test0|test02];
}
for(var option in optionArray){
var pair = optionArray[option].split(|);
var newOption = document.createElement(option);
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
}
</script>

More From » arrays

 Answers
17

You have to stringify the array



var test = <%- JSON.stringify(level_tab) %>;


I'm not familiar with EJS but in general the same principle should apply even if syntax is slightly different in EJS.


[#61523] Friday, July 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalistaz

Total Points: 0
Total Questions: 100
Total Answers: 106

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;