Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: children[i].children is not a function [duplicate]

I have a function that gets the all the li elements with the name "children-li" from a list "my_dynamic_list". each li element has three checkboxes of the names object_ck_bx, write_ck_bx, view_ck_bx, I need to get the values of that checksboxes, but I get "children is not a function", why the code can access to li tag name but can not access li children?

 function getTreeData() {
        // Get All Li elements thats contains the checkboxes form the type object,read,write
        var children = $('#my_dynamic_list').children().find('li[name=children-li]');
        var extracted_data = [];
        for (i = 0; i < children.length ; i++) {
            alert(children[i].tagName); //this gives li

            if (children[i].children().find('input[name=object_ck_bx]').checked == true) { // This Line gives Uncaught TypeError: children[i].children is not a function
                var profile_code = children[i].children().find('input[name=object_ck_bx]').id;
                var view = children[i].children().find('input[name=view_ck_bx]').checked;
                var write = children[i].children().find('input[name=write_ck_bx]').checked;
                alert('profile_code' + profile_code);
                var item = [];
                item = { Id: profile_code, read: view, write: write };
                extracted_data.push(item);
            }

        }
      //  alert(extracted_data);
        return extracted_data;
    }

The Html Code: enter image description here

The li properties in the browser looks like The li properties in the browser looks like

like image 831
userx Avatar asked Jan 28 '26 13:01

userx


1 Answers

When you call .children() for the first time, you get back a plain array of HTMLElement objects. The method .children() you are trying to call is a method of a jQuery collection. So in order to make a jQuery object from the plain HTMLElement you get returned from getting children[i], wrap that into the jQuery constructor, like so:

$(children[i]).children() // ...
like image 194
JJWesterkamp Avatar answered Jan 31 '26 02:01

JJWesterkamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!