document.getelementbyid stop when finding a space in array

1.1k Views Asked by At

when i alert the array, he return the hole value, but when i try to get it from getElementById, he stop after the first space!??

I know, it's probably simple, but i dont understand the difference between the 2 answers

https://jsfiddle.net/prodeinfo/jvywho3y/

html

<div>
<div  id="statusList"></div>
</div>

javascript

var countryLan = ["nothing","Also nothing",];;
alert(countryLan[1]);
document.getElementById('statusList').innerHTML += "<input type='text' value=" + countryLan[1] + " />";
1

There are 1 best solutions below

0
On BEST ANSWER

Try this:

document.getElementById('statusList').innerHTML += "<input type='text' value='" + countryLan[1] + "' />";

as you can see, I added ' in value: value='" + countryLan[1] + "' />"

You had a problem because if you want to pass a text that contains spaces, you need to quote this text properly. In your case, you just put the value from an array as is, without quotes and that's why there was only "Also" in your input.

Now it should work. Good luck.