0

Getting data from server as below

Test        Your    Code
New  Testing    Code
Opel Audi 

Above data have sapace between the words. i want to show text as it is.

I have html select dropdown where i am rendering the server data.

but i am not able to diplay data as it is coming form server, it is showing one space in between the word. can anyone tell me how to fix this?

<html>
<body>

<select>
  <option value="volvo">Test        Your    Code</option>
  <option value="saab">New  Testing    Code   </option>
  <option value="opel">Opel Audi</option>
</select>

</body>
</html>

check plunkr

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78

3 Answers3

1

HTML will remove any spaces following 1 space. Just replace the spaces with &nbsp; (non-breaking space point)

str = str.replace(/\s/gmi, '&nbsp;');

edit: forgot semicolon

Baruch
  • 2,381
  • 1
  • 17
  • 29
  • ` ` is the encoder. You can read more about `nbsp` here https://en.wikipedia.org/wiki/Non-breaking_space – Baruch Jul 23 '18 at 10:22
1

you can use string replace method

str = str.replace("", '&nbsp');

Anil Khatour
  • 57
  • 1
  • 5
1

Try whitespace characters &nbsp; instead of spaces. In your case you need to modify text coming from server accordingly.