November 20, 2011

How to change the label text in javascript function?

Question by user1047651

i want to validate login page using java script function.
i take one label button for displaying message and two text box for username and password and one button for “Login” .

when user click on “Login” button without enterning username and password then message will be display in label control. So how to check textbox value in javascript function and how to change the value in lblmessage. I call this function in Ok button . I set this function on Button OnClientClick event but this does not work.

Answer by Starx

With javascript

A simple example

var lblElement = document.getElementbyId("yourlabelsid");

lblElement.innerHtml("Your new updated text in the label");

But try to use a javascript libray like jquery, it has a lot of benefits and ease.
Generically on jquery, to change the markup of any element, we use

$("#theselector").html("the new markup inside the markup");

In case input elements, we use .val();
$(“#mytextbox”).val(“the value in the textbox”);

Based on your description, you want to change the text inside label so a simple example

For this label

<label id="lbl_name">Name:</label>


$("#lbl_name").html("the updated html"); 

Anywhere you need on the script

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!