Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8093469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:30:33+00:00 2026-06-05T20:30:33+00:00

I have this caption function that worlds great. If I fill in the alt

  • 0

I have this caption function that worlds great. If I fill in the alt attribute it creates a caption, and the data attribute as a link for the caption. Works great.

Issue is if I don’t have anything in the alt or data attribute, a div still appears. This is an issue, because as you can see in my CSS, the link has a background with padding, so with no data or alt attribute, a blank div appears with a black background.

I want to be able to change my function to say IF image has alt, display is along with the data info to create the caption, else, display the entire .caption to none.

I can’t figure out to have jQuery check to see if there is the alt. I want the ALT attribute to control whether or not the alt and data info show.

Here is what I have currently:

Jquery:

function imageCaption() {
    var $image =$('img');

    $image.wrap('<div class="box"></div>');

    $image.each(function() {
        var caption = this.alt;
        var link = $(this).attr('data');
        $(this).after('<div class="caption"><a href="'+ link +'" target="blank">'+ caption +'</a></div>');
    });
}

HTML:

<img src="IMAGE URL" alt="This is my first image" data="http://www.google.com">
<img src="IMAGE URL">

^^ The second image has the caption box rendered, with a blank link with padding.

CSS:

    #photoWrapper .caption {
    position: absolute;
    bottom: 10px;
    right: 5px;
    text-transform: uppercase;
    font-size: 10px;
}

#photoWrapper .caption a {
    text-decoration: none;
    color: #F1F1F1;
    padding: 5px 10px 5px 10px;
    background: rgba(0,0,0,0.7);
}

#photoWrapper .caption a:hover {
    background: rgba(0,0,0,0.9);
    color: #ff6666;
}

So to wrap it up, just looking to change my function with an IF statement that looks to see if the image has an alt attribute, and if so, show caption with link and text, and if not, hide caption.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:30:34+00:00Added an answer on June 5, 2026 at 8:30 pm
    $image.each(function() {
       if($(this).attr("alt")) {
           // The image has an attribute do your thing
            var caption = this.alt;
            var link = $(this).attr('data');
            $(this).after('<div class="caption"><a href="'+ link +'" target="blank">'+ caption +'</a></div>');
    
        }     
    });
    

    There is a similar question here: jQuery hasAttr checking to see if there is an attribute on an element

    That suggests an alternate way to make the check however I’m not sure the longer code is required.

    var attr = $(this).attr('name');
    
    // For some browsers, `attr` is undefined; for others,
    // `attr` is false.  Check for both.
    if (typeof attr !== 'undefined' && attr !== false) {
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}
I have this function // add history paths and save data function AddPath( strTag,
I have this little hover script. $('iframe').hover(function() { $('.caption').fadeIn(800); }, function() { $('.caption').fadeOut(800); });
I have this small script that shows a caption for a img when it
I have a string that looks something like this: {{imagename.jpg|left|The caption for this image.
I have a function that receives JSON data, it can be any length and
Hello I have an example that works properly: $(function() { $(#treegrid).jqGrid({ url: 'tree2.json', datatype:
I have a javascript function that gathers two arrays, imagepaths and captions. I want
I am trying to write a function that will output every other attachment caption
$(document).ready(function() { $(.module .caption).hide(); $(.module).hover(function() { $(this).find(.caption).slideDown(); },function() { $(this).find(.caption).slideUp(); }); }); This snippet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.