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
-
- Create a basic dot HTML file , complete with a Doctype, header and Body.
- Create a Div Tag with a Class name <fillText/strong>
- Embedd “Script “tag to wrap your JavaScript code
- at the Script tag assign the type“text/javascript” as you want your script to output a text
- Create a function call “myFirstScript”
- Target the HTML div tag ID with the class name “fillText”by using the getElementById() method
- Assign the getElementById() to a variable call setText
- setText.textContent with the .textContent method, assign the text value” “I am Flling in atext via Javascript”
<!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
check out how to program If else conditional statement in Java here
Leave a Reply