Issue
In my code, I am setting a variable called language
via JSTL
. This is how I do this.
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="c" uri= "http://java.sun.com/jsp/jstl/core" %>
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmt:setLocale value="${language}" />
<fmt:setBundle basename="text" />
I have a JQuery code, which will check the language
and display the content accordingly.
<script >
$(function() {
$('#SelecttheCategory').on('change', function() {
alert("${language}");
val = $(this).val();
$.get("DynamicPopProductList?categoryID=" + val, function(responseJson) {
var $dropdown = $("#Selecttheproducts");
$dropdown.empty();
$.each(responseJson, function(index, item) {
$dropdown.append($("<option />").val(item.idproduct).text($ {
language
} == 'si' ? item.sinhalaName : item.productName));
});
});
});
});
</script>
If the language is sinhala
I need to display item.sinhalaName
and if it is not, I am displaying item.productName
. Anyway, this do not work as expected. I do not get anything. However in my alert
, it shows the language
correctly as si
.
How can I fix this?
Solution
Assuming that the ${language}
value is output to the source on the server side then you need to wrap it in quotes for the Javascript syntax to work correctly. Try this:
var optionText = '${language}' === 'si' ? item.sinhalaName : item.productName;
$dropdown.append($("<option />", {
value: item.idproduct,
text: optionText
});
Answered By - Rory McCrossan