How can I add my java array contents into empty javascript array? I tried but its not working.
Here is my code in file.jsp.
<%@page import="javax.portlet.PortletPreferences" %>
PortletPreferences prefs = renderRequest.getPreferences(); // here i am reading values from preferences in Liferay and rendering values
<select id="dropdown1" onchange="changeAction(this.value);">
<option value="" selected="selected" >Select</option>
<option value="">value1</option>
<option value="">value2</option>
</select>
<label>Device</label>
<%
String device1 = prefs.getValue("device1","");
String[] array1 = device1.split("\n"); // String[] array=["books","pens","cars"];
%>
<select id="dropdown2">
<option value="select" selected="selected">Select</option>
</select>
below is my Java script code, both jsp code and javascripts are inside same jsp file.
<script>
function changeAction(optionVal){
var tempArrayLabel = [];
jQuery("#dropdown2").empty().append('<option value="-1" >Select</option>');
if(optionVal == "selected val in first drop down") {
tempArrayLabel = "add values from array1 or device1"; // update the drop down list
}
for (var i=0;i<tempArrayLabel.length;i++){
jQuery('<option/>').html(tempArrayLabel[i]).appendTo('#dropdown2');
}
}
</script>
Thanks for any kind of suggestion.