0

In javascript by this code

 var cartname+=cartArray[i].name;// cartArray[i].name returns names like 
                                    Orange, Banana, Apple

 document.getElementById("cartname").value= cartname; // sending to home.jsp by id cartname

In home.jsp


    <input type ="text" id="cartname"/> // got output like OrangeBananaApple 
                                            i.e. without commas

I tried to separate above with comma by following code:

      cartname+=cartArray[i].name;
      var cartname2=[cartname];
    document.getElementById("cartname2").value= cartname2.join(","); 

Still got no result. How to get output separated with commas like following in home.jsp

       Orange, Banana, Apple
Sambit
  • 7,625
  • 7
  • 34
  • 65
Tom
  • 761
  • 7
  • 22
  • 41
  • Hi I tried with above code but no output is coming, even I am not getting Orange Banana Apple output also – Tom Jun 08 '19 at 05:53
  • @Tom can you post the complete code, from where cartArray[i] is coming and is there's anything else you're doing with cartName before assigning to DOM element – Code Maniac Jun 08 '19 at 05:54
  • https://stackoverflow.com/questions/56487562/how-to-return-multiple-values-from-javascript-to-jsp?noredirect=1#comment99570062_56487562 – Tom Jun 08 '19 at 06:05

3 Answers3

1

You can add cartArray[i].name with a trainling space. Then while setting the value of the input trim the string use replcae to replace white space with ,

//' orange apple mango'; // string with white space before adding to value
let cartName6 =' '+cartArray[i].name;
console.log(cartName6.trim().replace(/ /g, ','))
brk
  • 48,835
  • 10
  • 56
  • 78
  • Hi I am getting single output like either Orange or Banana or Apple but not all . cartname+=cartArray[i].name; // += attribute is to be added I think – Tom Jun 08 '19 at 06:00
  • where can I add += attribute to display multiple fruit names – Tom Jun 08 '19 at 06:09
  • Hi thanks for helping me.. I tried like this cartname+=[cartArray[i].name]; document.getElementById("cartname").value= cartname.trim().replace(/ /g, ','); but did n't work actually, can u pls correct me – Tom Jun 08 '19 at 06:37
  • Why `cartname+=[cartArray[i].name]; ` instead of `cartname+=cartArray[i].name; ` ? – brk Jun 08 '19 at 06:44
  • ok I edited my final code 'var cartname=" "; ' cartname+=cartArray[i].name; document.getElementById("cartname").value= cartname.trim().replace(/ /g, ','); ' but getting same output i.e. OrangeBananaApple – Tom Jun 08 '19 at 06:48
  • @Tom i have again edited the answer for ease of your understanding. Please check – brk Jun 08 '19 at 06:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194628/discussion-between-tom-and-brk). – Tom Jun 08 '19 at 06:54
  • Hi brk pls come to chat I need little bit of help to write same in javascript – Tom Jun 08 '19 at 07:00
  • @Tom chat block in my location – brk Jun 08 '19 at 07:14
  • Hi brk there is no whitespace in output before. My output is like OrangeMangoApple like this. While running ur code here error is coming in output pls see – Tom Jun 08 '19 at 11:50
0

Here is the simple example for converting your array to string and joining it with ",".

<script>
 var fruits=["Apple","Banana","Orange"];
 document.write(fruits.join(','));
</script>

You can apply it on your own code,

document.getElementById("cartname").value= cartArray[i].name.join(','); 

=>output:

Apple, Banana, Orange.

kamasuPaul
  • 173
  • 1
  • 9
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
  • my output is var fruits=[AppleBananaMangoOrange] , there is no comma (,) separated . I have to separate with comma thats the question I have asked – Tom Jun 08 '19 at 11:53
  • Hey @Tom, Are you able to share whole cartArray so we may able to figure out why your code does not omitted the expected output. – Dushyant Tankariya Jun 08 '19 at 17:23
0

Use map,

cartname=cartArray.map(e => e.name).join(",");
Srinivasan Sekar
  • 2,049
  • 13
  • 22