Display ALT text as title within fancybox using jquery.fancybox

1.1k views Asked by At

Image HTML looks like this

<img  src="image.png" alt="Alt text" title="Description">

I have special function to output display Image title as filename with no extension and Description within fancybox.

My question is: how to get value from alt attribute and output it instead of filename(title_new) if alt is not empty.

jquery.fancybox-1.3.4.pack.js (full http://pastebin.com/bFmg95AT)

_format_title = function(title) {
            var nstr=imgPreloader.src;
            var nres=nstr.split("/");

            var img_name=nres[nres.length-1];
            var img_name_arr=img_name.split(".");
            var title_new= "<b>"+img_name_arr[0]+"</b><br>";

        if (title && title.length) {

                if (currentOpts.titlePosition == 'float') {
                    return '<table id="fancybox-title-float-wrap" cellpadding="0" cellspacing="0"><tr><td id="fancybox-title-float-left"></td><td id="fancybox-title-float-main">' +title_new+  title+ '</td><td id="fancybox-title-float-right"></td></tr></table>';
                }

                return '<div id="fancybox-title-' + currentOpts.titlePosition + '">' + title_new + title + '</div>';

            }
2

There are 2 answers

1
d-unit On

To get the alt attribute of an image you would use code like this where 'this' is the image you are using.

var alt = $(this).attr("alt");
2
Mick Redman On

This answer works on the basis that 'title_new' has already been declared and given a default value

if (typeof $('img').attr('alt') !== typeof undefined && $('img').attr('alt') !== false)
{
    title_new = $('img').attr('alt');
}