1

I wanted to fill a text area with some text as the user presses a button. But I have a problem that the variable to fill the text takes only single line input but I wanted to have some text with multiple lines and indentations

<!DOCTYPE html>
<html>
    <head>
        <title>SampleCodes</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <script type="text/javascript">
            function fill(){
                var text1 ="#include<stdio.h>
                            #include<conio.h>
                            void main()
                           {
                             printf("Hello World");
                           }"
              document.forms.myform.area.value=text1;
            }
        </script>
        <div>
            <button onclick="fill()">Program 1</button>
            <button>Program 2</button>
            <button>Program 3 </button>
            <button>Program 4</button>
        </div>
        <div class="wrapper">
            <div class="gfg">
                <form name="myform">
                    <textarea name="area" rows="30" cols="60">
                    </textarea>
                </form>
            </div>
        </div>
    </body>
</html>

2 Answers 2

2

You can simply use Template string ``

var text1 =`#include<stdio.h>
            #include<conio.h>
            void main()
              {
                printf("Hello World");
              }`
Sign up to request clarification or add additional context in comments.

Comments

1

In ES5 multiline text can be escaped with a backslash character (\).

var text1 = "#include<stdio.h> \
             #include<conio.h> \
             void main() \
             { \
                 printf(\"Hello World\"); \
             }";

If you are using ES6 you can use backticks to wrap multiline strings.

var text1 = `#include<stdio.h>
             #include<conio.h>
             void main()
             {
                 printf("Hello World");
             }`;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.