MyServlet forwards to Mypage.jsp as
request.getRequestDispatcher("/pages_homepage.jsp?value="+count).forward(request, response);
where count is an integer value generated
Below is my JSP code(Mypage.jsp),
<body onload="getPage('<%request.getParameter("value");%>')">
<div id="app"></div>
</body>
Below is my javascript code,
function getPage(match){
var arr = new Array();
var ele = document.getElementById('app');
for(var i=0;i<match;i++){
var newdiv = document.createElement("label");
newdiv.id = arr[i];
newdiv.value="Page";
ele.appendChild(newdiv);
}
}
What I want is that, I want 'Page' to be displayed 'match' number of times. But I'm not being able to do so by the above code. Their might be something wrong with my js code. Can anyone suggest me any corrections? Thanks in advance.
MyServlet forwards to Mypage.jsp as
request.getRequestDispatcher("/pages_homepage.jsp?value="+count).forward(request, response);
where count is an integer value generated
Below is my JSP code(Mypage.jsp),
<body onload="getPage('<%request.getParameter("value");%>')">
<div id="app"></div>
</body>
Below is my javascript code,
function getPage(match){
var arr = new Array();
var ele = document.getElementById('app');
for(var i=0;i<match;i++){
var newdiv = document.createElement("label");
newdiv.id = arr[i];
newdiv.value="Page";
ele.appendChild(newdiv);
}
}
What I want is that, I want 'Page' to be displayed 'match' number of times. But I'm not being able to do so by the above code. Their might be something wrong with my js code. Can anyone suggest me any corrections? Thanks in advance.
Share Improve this question edited Jan 11, 2014 at 16:25 codingrose 15.7k11 gold badges40 silver badges58 bronze badges asked Jan 11, 2014 at 15:45 SaumilSaumil 2,5176 gold badges34 silver badges54 bronze badges 6-
What is
arr
? what should it contain? Interesting your nomenclatures, you named a varnewdiv
that actually is a<LABEL>
element and you're trying to add avalue
attribute to alabel
element and anID
from an empty array.... Can you please enlighten us? – Roko C. Buljan Commented Jan 11, 2014 at 15:50 - @RokoC.Buljan I was trying something to give an unique id to newdiv, but I know I have have written a messed up code. Can you suggest anything for it? – Saumil Commented Jan 11, 2014 at 15:54
- you mean something like: stackoverflow./questions/10126395/… ? – Roko C. Buljan Commented Jan 11, 2014 at 15:54
-
What is returned by
<%request.getParameter("value");%>
? Can you please preformulate your question and make it more clear about what you actually want? – Roko C. Buljan Commented Jan 11, 2014 at 15:56 - @RokoC.Buljan <%request.getParameter("value");%> this returns a number. – Saumil Commented Jan 11, 2014 at 15:58
3 Answers
Reset to default 4LIVE DEMO
Taking in consideration that your page has something like:
<body onload="getPage(5)">
function getPage(n) {
var ele = $('#app');
var labels = ""; // An empty string will be populated with labels elements:
for(var i=0; i<n; i++){
labels += '<label id="'+ i +'"> Page </label>'
}
ele.append( labels ); // append only once outside the loop!
}
The result will be:
<label id="0"></label>
<label id="1"></label>
<label id="2"></label>
<label id="3"></label>
<label id="4"></label>
If you want to start from 1
instead of 0
use:
labels += '<label id="'+ (i+1) +'"> Page </label>'
Note: ID
starting with (/ containing only) a number - is only valid in HTML5
Your Code is working and i have tested it
Since you don't have any content in the label tag hence it is not visible in browser
Secondly a small error in 6th line of js code
newdiv.id = arr[i];
arr[i] is not given any value hence change it with
newdiv.id = i;
enjoy your code
Thanks everyone for their help but I think I got the answer,
Instead of
<body onload="getPage('<%request.getParameter("value");%>')">
I wrote,
<body onload="getPage('<%=Integer.parseInt(request.getParameter("value"))%>')">
But thanks everyone again for their useful pointers.