Add Text Via JavaScript

Add Text Via JavaScript

Add Text Via JavaScript

Add Text Via JavaScript.In this article we will step through how to add text into HTML div tag , using JavaScript.

Instead of using the alert();method which sometimes it might not work properly with chrome .In the  Sample code below , i will directly write the text into the HTML div tag using an embedded JavaScript inside HTML File.

JavaScript is widely adopted by most website on the Internet. No matter you are a front end or a back end developer, is good to know how to programme JavaScript .

Lets Step into the Script and learn how to add text using JavaScript

    1. Create a basic dot HTML file , complete with a Doctype, header  and Body.
    2. Create a Div Tag with a Class name <fillText/strong>
    3. Embedd “Script “tag to wrap your JavaScript code
    4. at the Script tag assign the type“text/javascript” as you want your script to output a text
    5. Create a function call “myFirstScript”
    6. Target the HTML div tag ID with the class name “fillText”by using the getElementById() method
    7. Assign the getElementById() to a variable call setText
    8. setText.textContent with the .textContent method, assign the text value” “I am Flling in atext via Javascript”

What is .textContent

<!DOCTYPE html>
<html>
<head>
    
</head>
    <p>Fill in the Text using Javascript</p>
    <div class="fillText"> </div>

    <body>
        <script type="text/javascript">
             function myFisrstScript(){
                 
                var setText=document.getElementById("fillText");
                 setText.textContent="I am Flling in atext via Javascript"
               /*  var text = div.textContent*/
                 
                 
             }
        
        
        </script>
        
            
    </body>

</html>



Your Out put
Javascript Output

check out how to program If else conditional statement in Java here

Leave a Reply

Your email address will not be published. Required fields are marked *

five × 5 =