Solved

Getting the value of a label using JavaScript

Posted on 2014-11-17
3
387 Views
Last Modified: 2014-11-24
I have a label on my page called lblActualProductionQuantity

Inside a repeater, I have 2 textboxes called tbTotalQuantity and tbQuantityPerDisplay.
(I also have a text box inside this repeater called testQty, which ultimately will not be used)

What I'm trying to do is as the user is typing, I want to make tbTotalQuantity (inside repeater) = tbQuantityPerDisplay (inside repeater) times lblActualProductionQuantity (Outside repeater)

When I run this code, it works fine. All of the controls however are inside the repeater

<script type="text/javascript">
    function closest(elm, className) {
        do {
            elm = elm.parentNode;
            if (elm.className !== undefined && elm.className.indexOf(className) > -1) {
                return elm;
            }
        } while (elm.className !== undefined);
        return null;
    }
    window.populate_num = function populate_num(elm) {
        var dm = closest(elm.parentElement, 'entry');
         dm.getElementsByClassName("tbTotalQunatity")[0].value = dm.getElementsByClassName("tbQtyPerDisplay")[0].value * dm.getElementsByClassName("testQty")[0].value;
    }
</script>

Open in new window


I want to replace testQty with lblActualProductionQuantity in my code. Here is what I've tried:

<script type="text/javascript">
    function closest(elm, className) {
        do {
            elm = elm.parentNode;
            if (elm.className !== undefined && elm.className.indexOf(className) > -1) {
                return elm;
            }
        } while (elm.className !== undefined);
        return null;
    }
    window.populate_num = function populate_num(elm) {
        var dm = closest(elm.parentElement, 'entry');
        dm.getElementsByClassName("tbTotalQunatity")[0].value = dm.getElementsByClassName("tbQtyPerDisplay")[0].value * document.getElementById("lblActualProductionQuantity").value;
    }
</script>

Open in new window


That doesn't work, but I figured the logic would make more sense if I posted it. So how do I get the value of the label lblActualProductionQuantity which is outside of the repeater?


TIA
0
Comment
Question by:mwmiller78
  • 2
3 Comments
 
LVL 14

Expert Comment

by:quizwedge
ID: 40448200
Since the label is inside of a repeater, there would be multiple elements with the ID of lblActualProductionQuantity. This shouldn't happen, so ASP.NET renames the IDs for you.

You could continue to use class names or in your javascript you can use the asp.net control name to get the client ID.

Code example (not mine) can be found at http://www.codeproject.com/Tips/578580/How-to-Get-User-Controls-ClientID-in-JavaScript-in

VB code:
Public Function getClientID() As String
        Return Controls.ClientID
End Function

Open in new window


Javascript example:
function FunctionName {
    alert(document.getElementById('<%=Uctest.getClientID()%>').value);
}

Open in new window

0
 

Accepted Solution

by:
mwmiller78 earned 0 total points
ID: 40453064
Got it. Labels do not have values.
Number(lblActualProductionQuantity.innerText) worked
0
 

Author Closing Comment

by:mwmiller78
ID: 40461735
I was not provided with a working solution and was able to figure it out.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now