SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Embeding div arround certain images

    I had a task of embedding a div arround some images and i came up with an simple jQuery function to do the job.
    I works as intended but i would like to get som feedback on the scripting. Is it the right way to go arround speedwise etc.?

    The reason i ask is because i would like to become better at writing jQuery.

    Code:
    jQuery.fn.addImgFrame = function($) {
    	$(this).each(function() {
    		var el = $(this);
    		var img_url = el.attr('src');
    		el.replaceWith('<div class="image-frame"><img src="' + img_url + '" alt="" /></div>');
    	});
    }
    	
    $('img.pic').addImgFrame(jQuery);

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Denmark
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    small korrektion

    Code:
    jQuery.fn.addImgFrame = function($) {
    	$(this).each(function() {
    		var el  = $(this);
    		var objAttr = [el.attr('src'), el.attr('alt')];  
    		el.replaceWith('<div class="image-frame"><img src="' +objAttr[0]+ '" alt="' +objAttr[1]+ '" /></div>');
    	});
    }
    	
    $('img.pic').addImgFrame(jQuery);


Tags for this Thread

Bookmarks

Posting Permissions

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