How can I access a jsp array in javascript function with in a single jsp page?
String[] abc[]={"saab","volvo","bmw","Benz","Porsche","Jaguar"};
abc[0]="saab";
How can I access a jsp array in javascript function with in a single jsp page?
String[] abc[]={"saab","volvo","bmw","Benz","Porsche","Jaguar"};
abc[0]="saab";
You should serialize it to json object first with for example FlexJson.
You could serialize java array to json array which should look like
[{item1, item2},{item},{item1,item2}]
with that method
String[] temp = ...
StringBuilder builder = new StringBuilder();
builder.append("[");
for(int i = 0 ; i < temp.length ; i++){
builder.append(temp[i]);
if(i != temp.length - 1 ){
builder.append(",");
}
}
builder.append("]");
String jsonArray = builder.toString();
and add this to your request;
then in your javascript use
var array = <%= request.getAttribute("temp")%>;
check if this is it with
console.log(array);
<%
String[] abc={'saab',' volvo',"bmw","Benz","Porsche","Jaguar"};
%>
<script language="JavaScript">
var jsArr = new Array();
<%
for (int i=0; i < abc.length; i++) {
%>
jsArr[<%= i %>] = '<%=abc[i] %>'; //Here is the latest update check it sravan .Put single quotes.
<%}%>
</script>
function myFunction()
{
var select = document.getElementById("dropDownId");
for ( var i = 0; i < jsArr.length; i++ ){
var option = document.createElement("option");
// set the text that displays for the option
option.innerHTML = status[i];
// add the option to your select dropdown
select.appendChild(option);
}