const el_Demo = document.getElementById("demo") // demo element function myFunction() { const stringSample = "how was your day?"; const arrayObject = stringSample.split(" ", 2); el_Demo.textContent = arrayObject; const textValue = el_Demo.textContent; el_Demo.textContent = textValue.toUpperCase(); }
<p id="demo"> ... result after click ...</p> <button class="btn" onclick="myFunction()">click</button>
If I split the comma , then the code results in the full words I've written in the str variable , and not only 2.
How do I get rid of the comma that appears?
If you join
it back with res.join(" ");
you will convert it from an array to a string with no comma:
document.getElementById("demo").innerHTML = res.join(" ");
You can use join(' ')
to join the array.
function myFunction() { var str = "how was your day?"; var res = str.split(" ", 2); document.getElementById("demo").innerHTML = res.join(' '); var txt = document.getElementById("demo").innerHTML; document.getElementById('demo').innerHTML = txt.toUpperCase(); }
<div id="demo"></div> <button class="btn" onclick="myFunction()">click</button>
Which produces
HOW WAS
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.