views:

23

answers:

1

I am trying to use the jQuery qTip plugin to display the text inside the element.

I have multiple links in a table like so with different classes. The text in the links is hidden with a negative text indent and a background sprite is used to make them look unique.

<a href="#" class="spot_quote">Spot quote</a>

<a href="#" class="contract_quote">Contract quote</a>

I thought this would jquery would just pick up the text in the link:

$('.contract_quote, .spot_quote').qtip(
   {
       content: $(this).text() // Give it some content, in this case a simple string
   });

But it returns way more than i need or want. I don't really want to add the title attribute as it is not needed here.

+1  A: 

JavaScript has no block-level scope. That means that your this refers to the document. In order for this to refer to each element, you need to create a new function:

$(document).ready(function() {  
  $('.contract_quote .spot_quote').each(function() {
    $(this).qtip({
     content: $(this).text();
    });
  });
});

That said, even if you'd need the title attribute, it would be good practice to set it anyway. Setting it automatically with JavaScript is OK but not ideal. Setting it with your CMS (if you use one) would be better. If a user agent, for some reason, is looking for the title of the link, it's just better to have one, even if it's always the same as the content, as user agents are not as smart as humans. Most of the times, anyway.

eje211
That's great. Much appreciated :)
RyanP13
I thought that is you had the title attribute set on a anchor and the text inside said anchor was the same then both would be read out which wold be potentially annoying?
RyanP13
The `title` attribute is optional, true, and I usually leave it out, which is bad. But with HTML, it's better not to think about what things *do* so much as to what they *mean*. If you made your titles bold, would you put a `<b>` tag inside an `<h2>` tag? No. You'd use CSS. Even if it looked good, a `<b>` inside an `<h2>` would not mean anything consistent. Here, it's the same. If *know* your links have a title, and it's easy to put it, put it. The *meaning* of it is what matters. If the browser does something annoying with it, the browser is the one at fault. The title is still meaningful.
eje211