jstl c:set is not working on js code in jsp file


jstl c:set is not working on js code in jsp file



I have a JSON from a service. I am appending a html block by JSON value and I have to check some values for appropriate display.
I am trying to set discountValue variable but it not working with js variable. How can I solve it?


if (data!="")
{
$.each(data,function(index,element){
var intDiscount=parseInt(10);
console.log("intDiscount::"+intDiscount);

var strIndex='';
strIndex+='<div class="card">';
strIndex+='<c:set var="discountValue" value="'+intDiscount+'"/>';
console.log("${discountValue}");
...
...
...
...
}
$('#visilabs-FavouriteCategoryTopSeller').append(strIndex);



Console log:


intDiscount::10
0





JSTL executes at server-side. JavaScript executes at client-side, long after the JSTL has ended generated the HTML + JS code. This makes no sense.
– JB Nizet
2 days ago





To add to JB Nizet's comment, this line: strIndex+='<c:set var="discountValue" value="'+intDiscount+'"/>' will always be the same as strIndex+=''; even outsize of an ajax JS call because c:set doesn't print out anything.
– Matt
2 days ago


strIndex+='<c:set var="discountValue" value="'+intDiscount+'"/>'


strIndex+='';





If you have already a JSON- result, there is no need for JSTL. If the JSON result is in data, you have to extract your value from data. With your $.each(data, function(index,element) you assume, data is an array. If so, you will be called for every arrayelement and have to use the element arg of your function.
– Holger
2 days ago


data


$.each(data, function(index,element)


element




1 Answer
1



I have solved it with using if condition with js:


if(element.discount>0){
strIndex+= ' <span class="label label-danger discount">'+'%&nbsp;'+intDiscount+'</span>';
}



Thanks everyone






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Boo (programming language)

How to make file upload 'Required' in Contact Form 7?