เพื่อจัดการแท็บ การขึ้นบรรทัดใหม่ และช่องว่างในข้อความ ให้ใช้คุณสมบัติ whiteSpace ใน JavaScript ตั้งค่าเป็น normal, nowrap, pre เป็นต้น เมื่อตั้งค่าเป็น pre จะทำงานเหมือนแท็ก <pre> ข้อความจะขึ้นบรรทัดใหม่เฉพาะที่มีการขึ้นบรรทัด
ตัวอย่าง
คุณสามารถลองรันโค้ดต่อไปนี้เพื่อเรียนรู้วิธีทำงานกับคุณสมบัติ whiteSpace ใน JavaScript
<!DOCTYPE html> <html> <head> <style> #box { width: 300px; height: 100px; border: 2px solid black; background-color: gray; } </style> </head> <body> <button onclick="display()">Set</button> <div id="box"> This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. This is Demo Text. </div> <script> function display() { document.getElementById("box").style.whiteSpace = "pre"; } </script> </body> </html>