.children().innerHtml on a JQuery object from a DOM node object returns undefined

user3298104
function getTaskProperties(node) {
        var data = {};
        data.name = $(node).children(".nameHere").innerHtml;
        data.date = $(node).children(".dateHere").innerHtml;
        data.class = $(node).children(".classhere").innerHtml;
        console.log($(node).children(".nameHere")); // returns something like:
        //[p.nameHere, prevObject: n.fn.init[1], context: div#entryTemplate.entry, jquery: "1.11.0", constructor: function, selector: ""…]

        console.log($(node).children(".nameHere").innerHtml); //returns undefined
        return data;    }

    function getAllTasks() {
        var tasks = [];
        $(".entry") .each(function (i, e) {
            console.log (getTaskProperties(e));
        })
        for (var i; i<tasks.length; i++) {
            console.log(tasks[i]);
        }
    }

This script runs through a series of HTML elements that look like the following:

<div class = "entry" id = "entryTemplate">
                <a class = "trashButton"><i class="fa fa-trash-o"></i></a>
                <p class = "classHere">History</p>
                <p class = "dateHere">Due: Monday</p>
                <p class = "nameHere">Graphic Organizer</p>
            </div>

There are also a couple other elements that get included in the JQuery selector for <.entry>

My problem is that when I run $(node).children().innerHtml it returns undefined. How can I properly get the value of .classHere, .nameHere, and .dateHere?

jfriend00

.innerHTML is a DOM property name (and you had it with the wrong capitalization).

$(node).children(".nameHere") produces a jQuery object.

.innerHTML is NOT a property of a jQuery object (it's a property of a DOM object) and thus is does not work on a jQuery object.


You can either use all jQuery:

$(node).children(".nameHere").html()

or you can fetch the DOM object from the jQuery object and then use .innerHTML as in:

$(node).children(".nameHere")[0].innerHTML

By way of explanation - a jQuery object and a DOM object are not the same thing. They are different types of objects that have different properties and methods.

A jQuery object contains an array of DOM objects (inside it). So, when you want to carry out an operation on the DOM objects, you can either use a jQuery method which will apply that method to the DOM objects inside the jQuery object or you can fetch the DOM objects out of the jQuery object and apply DOM methods/properties directly to the DOM objects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery object returns undefined

From Dev

jQuery .html() from object returns undefined

From Dev

jQuery .html() from object returns undefined

From Dev

How to get the this DOM Object from the $(this) jQuery Object?

From Dev

Comparing the InnerHTML of an object in jQuery

From Dev

Find a DOM object's node Name in jQuery

From Dev

JQuery object with children causes undefined with populating Kendo menu

From Dev

JQuery object with children causes undefined with populating Kendo menu

From Dev

Appending elements to dom returns [object Object] or wrong data from array

From Dev

$.each returns 'undefined is not an object'

From Dev

Function Returns undefined object

From Dev

Typescript returns undefined on object

From Dev

JSON object returns as undefined

From Dev

JQuery selector returns unrelated array instead of dom object

From Dev

DOM object not becoming a jQuery object

From Dev

jQuery's .children() method returns 'undefined'?

From Dev

Compare a DOM innerHTML value with an object property

From Dev

jquery autocomplete returns [object Object]

From Dev

Access to DOM in <object> with jQuery

From Dev

Returns [Object object] instead Dom string in Reactjs

From Dev

Returns [Object object] instead Dom string in Reactjs

From Dev

Cannot get values from javascript object during onclick (returns undefined)

From Dev

Ajax returns together undefined and valid data from a object

From Dev

JSON object returns undefined value

From Dev

JSON object returns undefined value

From Dev

jQuery returns window object

From Dev

Getting Parent node from Json object with Jquery

From Dev

Remove Children from Json object

From Dev

Undefined returned from object

Related Related

HotTag

Archive