Chase Ernst
Chase Ernst

Reputation: 1155

Uncaught TypeError: undefined is not a function Calling JavaScript Function

I am get the error: Uncaught TypeError: undefined is not a function when I am trying to call my Javacsript function displayValue().

Error message firing when function is called.

Here is my Javascript code:

    var HoC = 0;
    var Rate = 0;

    function getHoursOrCubes(input, control)
    {
        HoC = input;
        var answer = HoC * Rate;
        displayValue(answer);
    }

    function getRate(input, control)
    {
        Rate = input;
        var answer = HoC * Rate;
        displayValue(answer);
    }

    function displayValue(finalValue) {
        var listViewRef = document.getElementById('LV_Tickets');
        var elementArray = listViewRef.getElementsByTagName('Label');
        alert('this worked.');

        for (var i = 0; i < elementArray.length; i++) {
            var elementRef = elementArray[i];

            elementRef.value = finalValue;
            alert(elementRef.value);
        }
    }

I believe the line that is throwing the error is var elementArray = listViewRef.getElementsByTagName('Label');.

I have a listview inside a listview and I want to set the value of a label in the nested listview. So that is what I am trying to do here.

Here is my asp code for the ListView I am trying to access:

<asp:ListView ID="LV_Tickets" runat="server" DataSourceID="SQL_Tickets"  InsertItemPosition="FirstItem" OnPreRender="LV_Tickets_PreRender" DataKeyNames="TicketNum">

The getHoursOrCubes() function is called in a nested ListView's insertingitem template:

<asp:TextBox ID="HoursOrCubesTextBox" runat="server" style="height: 20px; width: 165px;" Text='<%# Bind("HoursOrCubes") %>' onchange="getHoursOrCubes(this.value)" />

Upvotes: 0

Views: 381

Answers (2)

Mouser
Mouser

Reputation: 13304

    var listViewRef = document.getElementById(<%= LV_Tickets.ClientID %>);
    var elementArray = listViewRef.getElementsByTagName('Label');

listViewRef is a nodeList that means it's a collection of nodes. To call the function getElementsByTagName you need a node. I selected the first node in the list by [0]. This should return a node on which getElementsByTagName can be called.

Upvotes: 1

Jared Hooper
Jared Hooper

Reputation: 881

You're trying to call a function that doesn't exist.

The functiongetElementsByName returns an IHTMLElementCollection. Use the tags method of that object to get what you want. There is no method for getElementsByTagName that operates on the IHTMLElementCollection.

Instead of

var elementArray = listViewRef.getElementsByTagName('Label');

try

var elementArray = listViewRef.tags('Label');

That should work for you.

Upvotes: 0

Related Questions