creating an array in jsp from list of objects [duplicate] - javascript

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I am trying to build an array of DemoNames.name attribute, dynamically in jsp, how can i create an array and then pass to my js file for further validation?
<c:forEach items="${demoNames}" var="DemoNames">
${DemoNames.name}
</c:forEach>

You'll want to make this code declaration somewhere in the html after the js file is declared.
<script>
var nameArray = [];
<c:forEach items="${demoNames}" var="DemoNames">
nameArray.push("${DemoNames.name}");
</c:forEach>
yourJSFileFunction( nameArray );
</script>
If you viewed source on this I believe you would have a push line for each of the names.

Related

How to access variables from the ModelAndView in JavaScript code [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 3 years ago.
I have a controller which returns a ModelAndView object
return new ModelAndView("operatorDetail", model);
I can then access the elements in the model object from my JSP page like this
<c:if test="${not empty operator and operator.regionId eq item.regionId}"
my question is, is there a way I can access those elements in my javascript code ?.
I thought about putting those values in an input element in HTML and make it hidden and access their values, but is there a more practical way?
thank you.
Yes of course you can have something like this. Declare var in jsp
<c:set var="yourJspVar" scope="session" value=" your object"/>
and in java-script set variable like
<script>
var jsVar = '${yourJspVar}';
alert(jsVar);
</script>

How to include a PHP variable in Javascript code? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
[1] http://imgur.com/a/Ny8f7 "html code"
[2] http://imgur.com/a/kzEN2 "php code"
I try to use the variables from php in javascript to generate a chart with them.
Try using an id e.g.
In your html
name='someName'>
Then use that ID in your JS.
Use can use something like this:
var data = '<?=$dataFromPhp?>';

How do i view a Google chart in a JSP page? [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I am trying to convert my List of Doubles in EL to a Array in JavaScript for use further on with Google Charts.
But i am stuck here in the snippet it sees the var prices as an array of Characters.
<head>
${requestScope.prices} <!-- gives [130.98, 130.84, 133.23, 130.32] -->
<p id="demo"></p>
<script type='text/javascript'>
var prices = "${requestScope.prices}";
document.getElementById("demo").innerHTML = prices[2]; //Gives 3
</script>
</head>
You can try it using JSTL. Firstly, fill js array this like and access it.
var prices = [
<c:forEach var="price" items="${prices}">
<c:out value="${price}" />,
</c:forEach>
];
document.getElementById("demo").innerHTML = prices[2];

How to retrieve setAttribute from servlet into JavaScript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

How to pass global JS var within regexp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
This is my regexp
regexp:{
spamcheck : /^[15]+$/
}
I need to do this
var spamcheck_sum = 15;
regexp:{
spamcheck : /^[spamcheck_sum]+$/
}
and
Side note: I cant use my spamcheck_sum var outside the js reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first?
working with cms here and js file placement is automatic if I use the cms js placements calls . Joomla
var spamcheck_sum = 15;
regexp:{
spamcheck : new RegExp("^["+spamcheck_sum+"]+$")
}

Categories

Resources