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.

  1. if ${first} list<string>, need convert concatenated single comma separated string. because ${first} printing list object.
  2. slice expects index number, passing string
  3. you not doing .value after document.getelementbyid
  4. 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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -