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
?
.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.
Comments