Click here to Skip to main content
11,933,621 members (55,399 online)
Rate this:
Please Sign up or sign in to vote.
See more: Javascript jQuery
I have written a function that returns an Array of elements from one div and each element has an Array of attributes (ID, position, type(image or text) and text or image source). I think the main problem is, that I am generating a selector in the runtime from the ID - $(#Counter).text() in for cycle and instead of a selector I have written there the variable selector.toString(), but it gives me for example a position normally, but it doesn't give the class - instead of some type, there is written undefined. And in this if condition it throws this error: Uncaught ReferenceError: Invalid left-hand side in assigment
if ($(selector.toString()).children().first().css("class") = "img") {

How to fix this error or how fix the whole function to do what I want?
Here is the whole function commented:
function getAllElements() {
            var elems = new Array();    //creates new Array for all elements in div "platno".
            for (var i = parseInt("0"); i < parseInt($("#Counter").text()); i++) {
                var selector = "#" + i; //creates a selector from actual i, which represents ID.
                var elem = new Array(); // creates an Array for all attributes of an element
                elem[0] = i;    //first is ID
                elem[1] = $(selector.toString()).position();    //second is position
                elem[2] = $(selector.toString()).children().first().css("class"); //third is class (text or image)
                if ($(selector.toString()).children().first().css("class") = "img") {
                    elem[3] = $(selector.toString()).children().first().css("src");  //fourth is image source
                else {
                    elem[3] = $(selector.toString()).children().first().text;   //or fourth is text of an element
                elems[i] = elem;    // adds an element to Array of all elements
            return elems;   //returns all elements

Thanks a lot for good solutions!

Pepin z Hane
Posted 21-Apr-13 3:50am
Edited 21-Apr-13 4:26am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

if ($(selector.toString()).children().first().hasClass(""img") {


if ($(selector.toString()).children().first().attr("class") == "img") {

NOTE1 : use '==' not '='
NOTE2 : generating dynamic selector is valid and possible too
like this

var i =3;
$("#" + i).show()
Pepin z Hane 21-Apr-13 11:56am
Don't you know how do I get class from HTML object, by jQuery? .css("class") doesn't work and how do I get image source? .css("src") doesn't work.
abforce 21-Apr-13 11:59am
Oops, use .attr("class") Have fun my dear

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100