0

I have the following code

var allRows = $('.myclass');
...
allRows.each(function() {   //now search through all the rows
          var className = this.attr("class");
          ...    
        });

I am getting an error message

Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr'

What is wronte with my code? I did a console.log on the allRows and it is a jquery object.

5 Answers 5

1

You should try this,

 var className = $(this).attr("class");// attr is jquery function
 console.log(className);

Full code

var allRows = $('.myclass');
...
allRows.each(function() {   //now search through all the rows
    var className = $(this).attr("class");// change this to $(this) here
    console.log(className);            
});
Sign up to request clarification or add additional context in comments.

1 Comment

of course, thanks! attr() is jquery method, so of course you have to first pass this to jquery.
0

change to this:

var className = $(this).attr("class");

Comments

0

You should change this to $(this):

 var className = $(this).attr("class");

Comments

0

You could also use className

var allRows = $('.myclass');
allRows.each(function () { //now search through all the rows
    var className = this.className;
});

Comments

0

You are not using "this" correctly. Below is given the correct way:-

    var allRows = $('.myclass');
       ...
    $(allRows).each(function() {   //now search through all the rows
       var className = $(this).attr("class");
       ...    
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.