SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get image attribute (height, width, alt, title)

    How to get image attribute (height, width, alt, title) when knowning image src.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by riddlevn View Post
    How to get image attribute (height, width, alt, title) when knowning image src.
    Create an img object and give us your image src, then once it has loaded get the image attributes that you need from it.

    If you need to work with one that's already on the page, but you don't know where it is, search for all of the img tags on the page than filter them for the one that has a matching src.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast OMGCarlos's Avatar
    Join Date
    Apr 2012
    Location
    Boston, MA
    Posts
    91
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yep. If you're using jquery you can get it like this:
    Code:
    var image_src = "foo.png"
    $( 'img[src="' + image_src + '"]' );
    It would be a little more involved with straight javascript though. The image has to actually be loaded in order for you to know it's dimensions, at least with javascript.


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
  •