Skip to content
Related Articles
Open in App
Not now

Related Articles

DHTML JavaScript

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 03 Jan, 2023
Improve Article
Save Article

DHTML stands for Dynamic HTML. Dynamic means that the content of the web page can be customized or changed according to user inputs i.e. a page that is interactive with the user. In earlier times, HTML was used to create a static page. It only defined the structure of the content that was displayed on the page. With the help of CSS, we can beautify the HTML page by changing various properties like text size, background color, etc. The HTML and CSS could manage to navigate between static pages but couldn’t do anything else. If 1000 users view a page that had their information for eg. Admit card then there was a problem because 1000 static pages for this application build to work. As the number of users increases, the problem also increases, and at some point, it becomes impossible to handle this problem. To overcome this problem, DHTML came into existence. DHTML included JavaScript along with HTML and CSS to make the page dynamic. This combo made the web pages dynamic and eliminated the problem of creating static pages for each user. To integrate JavaScript into HTML, a Document Object Model(DOM) is made for the HTML document. In DOM, the document is represented as nodes and objects which are accessed by different languages like JavaScript to manipulate the document. 

DHTML JavaScript

DHTML JavaScript

HTML document include JavaScript:: The JavaScript document is included in our html page using the html tag. <src> tag is used to specify the source of external JavaScript file. Following are some of the tasks that can be performed with JavaScript:

  • Performing html tasks
  • Performing CSS tasks
  • Handling events
  • Validating inputs

Example 1: Example to understand how to use JavaScript in DHTML. 

HTML




<h1>
    GeeksforGeeks
</h1>
<p id = "geeks">
    Hello Geeks!
</p>
<script>
    document.getElementById("geeks").innerHTML =
 "A computer science portal for geeks";
</script>            

Output: 

javascript 

Explanation: In the above example, change the text of the paragraph using id. A document is an object of HTML that is displayed in the current window or object of DOM. The getElementById(id) gives the element id. The innerHTML defines the content within the id element. The id attribute is used to change an HTML document and its property. Paragraph content changed by document id. For example document.getElementById(“geeks”).style.color = “blue”; It is used to set the paragraph color using the id of elements. 

Example 2: Creating an alert on click of a button. 

HTML




<h1 id = "para1" 
    style="display: flex; margin: auto; justify-content: center;">
    GeeksforGeeks
</h1>
<input type = "Submit" 
       onclick = "Click()"
       style="display: flex; margin: auto; justify-content: center;"/>
<script>
    function Click() {
        document.getElementById("para1").style.color = "green";
        window.alert("Color changed to green");
    }
</script
              
                   

Output: 

 

Explanation: In this example, creating a function that will be invoked on click of a button and it changes the color of text and display the alert on the screen. window is an object of current window whose inbuilt method alert() is invoked in Click() function. 

Example 3: Validate input data using JavaScript. 

html




<style>
    body {
        margin-left: 50%;
    }
  
    h1 {
        color: green;
    }
</style>
<h1>
    GeeksforGeeks
</h1>
<h4>
    DHTML JavaScript
</h4>
<p>
    Enter graduation percentage:
</p>
<input id="perc">
<button type="button" onclick="Validate()">
    Submit
</button>
<p id="demo"></p>
<script>
    function Validate() {
        var x,text;
        x=document.getElementById("perc").value;
        if(isNaN(x)||x<60) {
            window.alert("Not selected in GeeksforGeeks");
        } else {
            document.getElementById("demo").innerHTML=
                "Selected: Welcome to GeeksforGeeks";
            document.getElementById("demo").style.color="#009900";
        }
    }
</script>

Output:  

 

Explanation: In this example, make a validate() function that ensures the user is illegible or not. If the user enters > 60 then selected otherwise not selected.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!