views:

874

answers:

7

Hello, I'm trying to apply styling to all elements with a title attribute. For some reason this code is only returning the first, not all. I'm probably missing something obvious... Any help would be great, thanks!

$("[title]").each(function() {

   doSomething(this);

}
+1  A: 

It may just be returning the TITLE element of the page. Try:

$("*[title]").each(blah);

EDIT: On testing, this will do what you want, but so will what you actually had, so it has to be something with your HTML. For reference, here's what I used:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title>Blah</title>
  <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;


  <script>
  $(document).ready(function(){
    var titles = $('*[title]');
        alert(titles.length);
  });
  </script>
</head>
<body>
  <a title="blah" href="#">blah</a>
    <a title="blah" href="#">blah</a>
    <a title="blah" href="#">blah</a>
    <a title="blah" href="#">blah</a>
    <a title="blah" href="#">blah</a>
</body>
</html>

Using just var titles = $('[title]'); returned 5 elements, as it should have.

Chris Doggett
+2  A: 

You need to close your each();.

simplemotives
+1 Without the closing ); it just breaks.
googletorp
This is right, but if he actually had it like that the code wouldn't even run... (he is getting only the first match)
Paolo Bergantino
+6  A: 

Your code is correct (aside from the missing ); I assume is just a pasting mistake) and should work.

The only reason why it wouldn't work is if you don't have your code wrapped around $(document).ready. If you are not familiar with $(document).ready it is a special event that jQuery has that fires as soon as the entire DOM structure is ready to be manipulated. If you have your Javascript at the top of your page anything you run outside of $(document).ready will be acting against an empty DOM because the browser has not had time to create the nodes yet. So whenever you want to manipulate elements (especially a query to get all the elements that have a title) you definitely need to put the code around this special event.

Keeping that in mind, I tried the following code, which would put a red border around elements with a title:

$(document).ready(function() {
    $("[title]").each(function() {
        $(this).css('border','1px solid red');
    });
});

And it worked properly.

Paolo Bergantino
+1 Good example w/ URL.
simplemotives
+1  A: 

A great tip to try first if strange things are happening with selectors is to check the rendered markup can actually validate at w3c. without a valid dom you can expect weird and wonderfull things from any dom scripting and selectors.

redsquare
+2  A: 

I just ran into this issue. Could it be that you're using an old version of the jquery.validate library as identified in this post?

For me, downloading & applying the latest version of the plugin worked

spmason
Barely found this, and your post resolved my issue, thanks!
DennyDotNet
A: 

THANK YOU STEVE! That completely fixed it. How frustrating! Thanks a lot.

Feel free to accept his answer by clicking the `V` sign. Also, answers as comments are discouraged.
Kobi
A: 

Thank you so much Steve, I spent a few days trying to figure out why the jquery selectors and jQuery.find() function were only returning the first element instead of all the elements that match the selector and you are right it was the jquery.validate plugin, I updated it and everything went fine. Thanks for your help.

Medhat Gayed