views:

61

answers:

4

I want to add and repeat title="some text" same as alt="some text" if title is already not present. To show Alt text in FF also.

before

alt="some text"

alt="some text" title="some another text"

After

alt="some text" title="some text"

alt="some text" title="some another text"

**I need *

jquery(with no conflict)

  • or simple javascript code.**
+2  A: 

Just as a side-note:

The contents of an ALT attribute should not be a tooltip (even though IE displays it like this, which it shouldn’t). The text should be short (less than 100 words, as per WCAG 2.0). The text should also be relevant to the primary content so users may use it INSTEAD of the image and still gain the image’s value. In this case, it’s an accessibility thing rather than a usability thing.

Source: Proper Use Of ALT And TITLE Attributes

The title attribute, on the other hand, is meant to provide additional information about an element.

Daniel Vassallo
Thanks for article although i know this very well, but for a reason i still want to use
metal-gear-solid
A: 

Something like this?

$("img[title='']").each(function(){
    $(this).attr('title',$(this).attr('alt'));
  });
o.k.w
A: 
$.noConflict();
jQuery(function(){
    jQuery("img.classname").each ( function(){
        var elem = jQuery(this);
        if ( elem.attr ( "title" ) === "" )
        {
            elem.attr("title", elem.attr("alt"));
        }
    });
});

See Using jQuery with Other Libraries

rahul
+1  A: 

self invoking function, safe with jQuery as parameter for conflicting $ variable

(function($){
    $('img').each(function () {
        var 
            $this = $(this),
            altText  = $this.attr('alt');
        if ($this.attr('title') === '') {
            $this.attr('title', altText);
        }
    });
})(jQuery)
Juraj Blahunka
will it conflict with prototype?
metal-gear-solid
what is (jQuery) at end of code? is it a part of code?
metal-gear-solid
that is self invoking function, jQuery object will be passed as parameter, so no conflicts will issue (no conflict with Prototype)
Juraj Blahunka
@Jitendra, see http://api.jquery.com/jQuery.noConflict/
o.k.w
working thanks. but i haven't tried solution of others
metal-gear-solid