Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.getElementsByClassName().innerHTML always returns "undefined"

I must have made a mistake somewhere so the document.getElementsByClassName().innerHTML is always returning undefined.

First i generate the <li> via javascript :

$('#list').append('<li class="box"><img class="picture" src="images/HotPromo/tagPhoto1.png"/><p class="name"><b>Name</b></p><p class="address">Address</p><p class="hidden"></p></li>');

Note that in the most right i have a <p> element with hidden class. I use this to get the id which i dont want to show to my users.

And this is the jQuery to generate the data on those <li> :

$(".box").each(function () {
    var name, address, picture, id = "";
    if (i < result.length) {
        name = result[i].name;
        address = result[i].address;
        picture = result[i].boxpicture;
        id = result[i].mallid;
    }

    $(this).find(".name").html(name);
    $(this).find(".address").html(address);
    $(this).find(".picture").attr("src", picture);
    $(this).find(".hidden").html(id);
    i++;
});

I have tried to check the data, and its working fine.

Now, lets say i want to alert the hidden id <p> when user clicks one of those <li class="box"> that i generated above:

$(".box").click(function () {
    alert(document.getElementsByClassName('hidden').innerHTML);
});

However this alert always returning "undifined".

like image 353
Blaze Tama Avatar asked Jul 27 '13 10:07

Blaze Tama


People also ask

What does getElementsByClassName () function return?

getElementsByClassName() The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.

In what format does the getElementsByClassName () method return its values?

The getElementsByClassName() method returns an HTMLCollection.

Why you shouldn't use innerHTML?

The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies. You can read the MDN documentation on innerHTML .

What is the difference between getElementById and getElementsByClassName?

getElementById returns a single DOM element whose ID matches your query. getElementsByClassName returns an HtmlCollection - an array-like structure containing the DOM elements that matched your query. You have to iterate through each element in the array to apply your style.


1 Answers

document.getElementsByClassName() returns a nodeList, not an element!

So it should be :

document.getElementsByClassName('hidden')[0].innerHTML 

and as you probably have more .hidden elements, and only want the one inside the current .box (which would be this in the event handler)

this.getElementsByClassName('hidden')[0].innerHTML 

but why not jQuery

$(".box").click(function(){         alert( $('.hidden', this).html() ); }); 
like image 190
adeneo Avatar answered Sep 29 '22 19:09

adeneo