2

I have this jquery code:

$.each('.button.gobutton', function() {
      alert($(this).attr('title'));
 });

I basically want to iterate through each element and get the value of the title for each element. However when I run this code I get undefined for all the values. Below is the html:

<button class="button gobutton" title="2">GO</button>
2
  • Whoops, I updated it, still doesnt work with gobutton Commented Oct 10, 2011 at 20:01
  • Have you tried console.log($(this)) inside the loop to see if the selector is working fine? Commented Oct 10, 2011 at 20:04

1 Answer 1

7

$.each iterates over arrays/objects. It doesn't select DOM elements.

It seems like you're trying to do this:

$('.button.gobutton').each(function() {
      alert($(this).attr('title'));
});
Sign up to request clarification or add additional context in comments.

3 Comments

This worked, will mark it as the answer once it lets me. Quick question, is the each selector considered optimal to go through lets say 100 entries every couple of seconds? I basically want to run code for each interation every 5 seconds.
@John - When doing such heavy lifting, it's always better to use native JavaScript. However, barring that, you should at least cache the selected items: jsfiddle.net/P6Vh5
Well then let me ask you then, if I may, what would you recommend the best solution to have a list of table rows displaying data from the db and each row has a create date associated to it. I want to display the difference between the create date and the current date in minutes for each entry in real time. Would this each work or is that going to really slow things down?

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.