Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript jQuery
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
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
v2

1 solution

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

Solution 1

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

 
or
 
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()
  Permalink  
v3
Comments
Pepin z Hane at 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 at 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
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
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