jsp - Getting values from list<String> , split them and then slice them by parts, using JavaScript -
i have list<string>
spring mvc want split, slice , print on browser. problem need enter start , end argument of slice() method variable text-field. code, doesn't work. can helps me that? code:
<body> <form>first value: <br/> <input type="text" id="firstvalue" />last value: <br/> <input type="text" id="lastvalue" /> <button onclick="myfunction()">press</button> <p id="demos"></p> </form> <script> function myfunction() { var str = "${first}"; var arr = str.split(","); var first = document.getelementbyid('firstvalue'); var second = document.getelementbyid('lastvalue'); document.getelementbyid("demos").innerhtml = arr.slice('first', 'second'); } </script> </body>
thank in advance!
you got issues in code.
- if
${first}
list<string>
, need convert concatenated single comma separated string. because${first}
printing list object. - slice expects index number, passing string
- you not doing .value after document.getelementbyid
- you not passing user input variables first , second slice, instead passing hardcoded strings
'first'
,'second'
.
below fixed code
html
<form>first value: <br/> <input type="text" id="firstvalue" />last value: <br/> <input type="text" id="lastvalue" /> <button onclick="myfunction(event)">press</button> <p id="demos"></p> </form>
js
var myfunction = function (e) { var str = "${first}" // assuming contains string "1,2,3,4,5,6,7,8,9,10"; , not list obect var arr = str.split(","); var first = document.getelementbyid('firstvalue').value; var second = document.getelementbyid('lastvalue').value; document.getelementbyid("demos").innerhtml = arr.slice(parseint(first, 10), parseint(second, 10)).tostring(); e.preventdefault(); };
Comments
Post a Comment