Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Javascript to get ASP.NEt Web Forms label's value?

I have the following label control:

<asp:Label ForeColor="DarkGreen" runat="server" ID="lblStatus"></asp:Label>

Its value is filled in the Page_Load event.

I attached the following Javascript (placed at the end of the page, not Master page):

function Validate() {
        var lblObj = document.getElementById('<%=lblStatus.ClientID%>');
        alert(lblObj.value);
        if (lblObj.value == "Replaced" || lblObj.value == 'Trashed' || lblObj.value == "Internal Use") {

            alert("Products with" + lblObj.value + "status cannot be reserved");
            return false;
        }
    }

The alert(lblObj.value) displays a popup with text "undefined". How can I fix this problem? Please, I tried many combinations for placing the JavaScript but no luck! Thanks

UPDATE

Browser soruce code:

<span id="ctl00__main_lblStatus" style="color:DarkGreen;">Available</span></td>

First line of Validate JS function:

function Validate() {
        var lblObj = document.getElementById('ctl00__main_lblStatus');
like image 550
CiccioMiami Avatar asked Dec 04 '22 07:12

CiccioMiami


2 Answers

labels don't have a value. They have innerHTML and innerText.

like image 78
Daniel A. White Avatar answered Jan 01 '23 11:01

Daniel A. White


Use JQuery and it will run in all browsers and platforms, something like:

$('#<%= lblStatus.ClientID %>').next().text();

source: JQuery: getting the value/text/innerHtml of a checkbox in an ASP.NET CheckBoxList control

like image 42
Davide Piras Avatar answered Jan 01 '23 11:01

Davide Piras