Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
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
JavaScript
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:
JavaScript
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
Updated 21-Apr-13 3:26am
v2

1 solution

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


or

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


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

JavaScript
var i =3;
$("#" + i).show()
 
Share this answer
 
v3
Comments
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.
Ali Reza Barkhordari 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900