How to iterate over htmlCollection

I have some difficulties with this. In Backbone, I have a function like this:

 functionOne: function(){
        $('#myTExtbox-' + budgetLine.attr('id')).on('change keyup paste', function(){
           that.mySecondFunction(this);
        });
 }

In this case, thisis textboxthat which is in table, inside a div. Then:

mySecondFunction: function(tb){
       var tbody = tb.parentElement.parentElement.parentElement.parentElement.parentElement;
       //gets main parent, which is a tbody, inside a table, inside a div

}

Then I want to iterate through tbodyto go through each line and find textboxin a specific cell. The problem is that this code:

    $.each(tbody, function(index, item){
        cost = item;
        var t= index;
    });

I don't seem to be able to get to any items. In this example, if I try to do something like:

  item.getElementById('test');

I get an error message:

TypeError: Object #<HTMLCollection> has no method 'getElementById'

Why can't I iterate over this object and access the objects inside?

thank

UPDATE

Here's the script: http://jsfiddle.net/HX8RL/14/

, : tb Tb. tb , , , tb.

+4
3

TBody children()

$.each(tbody.children('tr'), function(index, item){
        cost = item;
        var t= index;
    });

-

+2

.

var tbody = tb.parentElement.parentElement.parentElement;
    alert(tbody.id);
    var input = $('#tbody').find('input');
    alert(input);
    console.log(input);
    for (var i = 0; i < input.length; i++) {
        alert(input[i].value);
        alert(i);
    }

fiddle- http://jsfiddle.net/HX8RL/18/

+1

, - . , ? document.getElementByid('test').

jQuery, find, item.find('#test'). , . , , , , .

Also

tb.parentElement.parentElement.parentElement.parentElement.parentElement;

can be written as (in jQuery)

$(tb).parents('tbody');

I installed fiddle , maybe this can help you.

The code used in the script:

var myFuncs = (function() {

    function funcA() {
        $('input').on('keyup', function() {
           funcB(this); 
        });
    }

    function funcB(myInput) {
        var $table = $(myInput).parents('table');

        $table.find('tr > td > input').each(function() {
           var $input = $(this);

            if($(myInput).attr('id') != $input.attr('id'))
                $input.val("I'm called from another input");
        });
    }

    return {
        funcA : funcA
    }

})();

myFuncs.funcA();
0
source

Source: https://habr.com/ru/post/1533719/


All Articles