Solved

jQuery: Get Actual Width of Image

Posted on 2013-06-24
5
629 Views
Last Modified: 2013-06-24
How can I get the ACTUAL width of an image, not the width of the image as it is displayed?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
 $('img:eq(0)').attr('src', $('img:eq(0)').attr('src')).load(function () {

   alert('The width of the image as it is displayed  is: ' +  $('img').width() );
   alert('The actual width of the image is: ' +  '???' );
   //  Should be 2272 

 })
});

</script>
<style type="text/css">
html, body, p, img {
width: 100%;
padding: 0;
margin: 0;
}
</style>

</head>
<body>

<p>
 <img src="http://upload.wikimedia.org/wikipedia/commons/7/7b/Pic-du-midi-dossau.jpg" alt="" />
</p>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39272283
var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;

Open in new window

0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39272297
sorry you want the real size image and not the resized/rendered one

2.272px × 1.704px
0
 
LVL 16

Author Comment

by:hankknight
ID: 39272330
Does anybody else have a way to solve this using jQuery?
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39272408
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39272466
Great picture Gary - was over in France last week planning to climb this but the weather put a stop to it :(
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question