All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Set Label Value In JavaScript Dynamically

Last Updated : Mar 11, 2024

How To Set Label Value In JavaScript Dynamically

In this tutorial we will show you the solution of how to set label value in JavaScript dynamically, here we collecting label value by using getElementById() method as we know this is widely help us in script we can retrieve all html form elements values too easily.

Then using setTimeout() function we changing that label value to user given value dynamically and displayed result on console it will updated also on webpage.

Step By Step Guide On How To Set Label Value In JavaScript Dynamically :-

Here we defined label with id attribute ‘lbl’ and it had some text. We defined input tag for changing label value dynamically and a button for initialize function fun() call.

In script we defined variable ‘r’ for storing label value and we can display that label value using ‘innerText’ properties in console.log() method.

Using input tag id ‘inpt’ we collecting user gave value then we changing that label value dynamically after 3 seconds by using setTimeout() function.

<!DOCTYPE html>
<html>
    <head>
        <title>SET LABEL VALUE</title>
    </head>
    <body>
        <label id="lbl">Welcome message from label</label>
        <input type="text" placeholder="Enter Text for Label" id="inpt">
        <input type="submit" value="Change Label" onclick="fun()">
        <script>
           function fun(){
           var r=document.getElementById('lbl');
           var ipt=document.getElementById('inpt').value;
           console.log("Initial label text is : "+r.innerText);
           setTimeout(()=>{
            document.getElementById('lbl').innerText=ipt;
            console.log("After 3s label text is : "+r.textContent);
           },3000);
           }
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined label tag with id ‘lbl’ and within that we specified some text of ‘Welcome message from label’.
  7. Two input tags defined first one ‘inpt’ is for collecting dynamic value from user and another one ‘Change Label’ is for initialize function call fun().
  8. In script we defined function fun() here we defined variable ‘r’ for retrieve and display label ‘lbl’ value, ‘ipt’ for collect users dynamic label value then initial label value displayed on console.
  9. We defined setTimeout() function for user clarification so we can see result clearly when we changing label value suddenly users can’t understand if we using time delay we can understand easily.
  10. Here we inserting user provided value as label value by ‘innerText’ and displayed label value it will automatically displayed on both console, webpage.
  11. For seeing result we need to open console panel on browser so we have right click on browser webpage and select ‘inspect’ option then console panel will opened successfully.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to set label value dynamically using JavaScript.

When we executes program on browser we can see the label text ‘Welcome message from label’ on webpage.

For collect and displaying label value we need to use javascript properties as seen above.

After 3 seconds label value get updated as per user provided text.

Then for open console panel we had another option use shortcut ‘ctrl+shift+j’ on browser and at right hand side console panel will open with result.

I hope this tutorial on how to set label value in JavaScript dynamically helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪