Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jquery: creating image caption from alt

    hi,
    I am trying to create image caption by getting the value from alt in each img tag. i manage to get the alt value from each img tag with each();.

    and i use insertAfter() to insert the alt value after each image, but it turns out very strange, have a look from the link below,

    http://lauthiamkok.net/tmp/jquery/jquery.image_caption/

    it create a list of captions instead!

    what have done wrong to make this image caption function?

    this is the code i made so far,

    Code:
    $("#content img").each(function () {
    	var title = $(this).attr("alt");
    	$(document.body).append("<div class='caption'>"+ title +"</div>");	
    	$(".caption").insertAfter("#content img");
    	});
    });
    many thanks if u have any idea.

    thanks,
    Lau

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Code:
    $("#content img").each(function () {
    	var $this = $(this);
    	var title = $this.attr("alt");
    	$this.after('<div class="caption">'+ title +'</div>');
    });
    Untested, but that should do it.

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spudhead View Post
    Code:
    $("#content img").each(function () {
    	var $this = $(this);
    	var title = $this.attr("alt");
    	$this.after('<div class="caption">'+ title +'</div>');
    });
    Untested, but that should do it.
    just tested - it works perfectly. thank you very much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •