views:

33

answers:

3

what do you guys think of this code to help with some lazy people out there :)

$('a').each(function (i,n){
    $title = $(n).attr('title');
    if(!$title){
        $(n).attr('title',$(n).html());
    }
});

Considering that most links have the same title as their text so... let me knwo if any of you out there like this?

So we can tell w3school one box ticked :)

+1  A: 

it's unnecessary work for the browser. if you have a large number of links this code could slow down init time considerably.

However, if you want to speed it up, you could hide all your text (ie, take it out of the DOM) , apply this code, and then show it again. This may flash your text, though.

Also, just don't be lazy and put in a title :)

Jason
i agree, it just hits the DOM in a loop.
Prashanth
im not lazy always use the title just thought the concept would help those who are :)
Val
+1  A: 

You should prefix your variable with a var statement to prevent it being an implied-global.

Your whole thing can be simplified to:

$('a:not([title])').attr('title', function () {
    return $(this).text();
});

(preferring text() over html()).

Matt
A: 

What is your end goal? Are you just trying to satisfy some arbritrary checklist? Or are you actually trying to make your site more accessible to users? Have you tried an accessible browser - have you confirmed that they don't just use the link text on links that don't have a title attribute set? If that is the case you haven't actually achieved anything, and have just made your site slower for everyone.

If you want to support accessibility, please do it properly and use a properly thought out title attribute.

kwyjibo
Like i said this is for some lazy people out there meaning not myself.you can always use a title will not effect it at all. you could put that the very end of the page or on page load ... therefore it will not slow down the website at all.it's also logical that an element must exist before tempering with it. once the page is loaded that code can kick in therefore will not slow the page at all it's a background process.
Val
@kwyjibo +1 @Val A title is meant to convey supplementary information to the link text. A title identical to its link text brings NO information thus is useless thus shouldn't exist. I don't know anything about this W3schools box you think you've to tick but if it asks for `title`s everywhere, it's plain wrong. WCAG 2.0 Techniques [H30](http://www.w3.org/TR/WCAG-TECHS/H30.html) and [H78](http://www.w3.org/TR/WCAG-TECHS/H78.html) to H81 certainly don't promote this. EDIT: sorry to tell you that you can trash your script, it's of no help to people who need it.
Felipe Alsacreations
And I'll add that titles identical to link text could be worse than useless, in fact be counter-productive by distracting users. A screen reader user who hear two or three links where this is the case will switch off title rendering and then miss the useful titles where they're present in the HTML code!
Felipe Alsacreations