Code:
<% String r="4";
String b="5;
%>
<script>
var a={"red":"<%=r%>","blue":"<%=b%>"};
</script>
Is it possible to do like this and how to display those elements?
Yes you can do that, You can use hasOwnProperty
to get key and value.
<% String r="4";
String b="5";
%>
<script>
var a={"red":"<%=r%>","blue":"<%=b%>"};
for (var k in a){
if (a.hasOwnProperty(k)) {
alert("Key is :" + k + ", value is :" + a[k]);
}
}
</script>
You can make it as JS object if you use eval
function. Hope this helps.