0

I have a problem with IE8 ,I am getting the height of the object 0. It works fine in FF and chrome.

    var value = obj.prop("height");

prop() doesnot work with IE or, the height is 0 because the obj is a hidden image tag. Any help?

kennypu
  • 5,950
  • 2
  • 22
  • 28
Vidia
  • 43
  • 1
  • 2
  • 8
  • r u able to get the height of visible image on IE? –  Apr 21 '14 at 04:35
  • what is obj? I don't think prop is a vanilla javascript function – kennypu Apr 21 '14 at 04:36
  • If the element has a `prop` attribute, you should use `.attr()`, not `.prop()`. And if you want to get the calculated height, you should use `.height()`. – Barmar Apr 21 '14 at 04:50
  • @kennypu obj is the id of the image tag .. – Vidia Apr 21 '14 at 04:50
  • you might want to show relevant HTML. is there a reason you are using prop() to get height? you can just use .height() – kennypu Apr 21 '14 at 04:54
  • if i use obj.height() , i get the value 0 :-/ – Vidia Apr 21 '14 at 04:59
  • If the object is an image, you will need to make sure the image has finished loading BEFORE you request it's height. The only way to know for sure that the image has finished loading is to either check the `.complete` property or set an `onload` handler so you get notified when it finishes loading. The image height is simply not known until the browser has finished downloading the image. – jfriend00 Apr 21 '14 at 05:42

2 Answers2

0

I'm going to assume that you are trying get the height attribute from your img tag that look something like this.

<img src="http://placehold.it/350x350" width="350" height="350" style="display:none;">

Note the width and height attribute.

If you're trying to get the height attribute value in the img tag, try using .attr() like so

var value = obj.attr("height");

Another note is that the image might not be loaded and as a result is acting weird on IE8. Try to make sure the image is loaded before getting the height.

For this matter, see explanation about document.ready and window.load.

However, as many have said, you should be using .height() to get the height of an element.

Community
  • 1
  • 1
josephting
  • 2,617
  • 2
  • 30
  • 36
-1

.prop is a jquery function you need to include jquery in your page

http://api.jquery.com/prop/

for chrome and firefox you might have it it your browser's cache i think thats wht its working...

k961
  • 577
  • 1
  • 5
  • 19
  • I wouldn't post an answer, as there are still too many unknowns. you are assuming 1. he is using jquery and 2. if he is, that he doesn't have it included. we don't know that yet. – kennypu Apr 21 '14 at 04:38
  • I've included jquery too. – Vidia Apr 21 '14 at 04:41
  • @kennypu If it works fine on FF and Chrome, it should be jQuery. There isn't `.prop()` method in JavaScript. – josephting Apr 21 '14 at 04:45
  • i dont get any errors in FF , it works fine. There are no errors in IE console too, but the height is 0. – Vidia Apr 21 '14 at 04:46
  • @Vidia you should mention jQuery then, as .prop() is not from javascript. – kennypu Apr 21 '14 at 04:48
  • check your docype see if its valid ! its IE – k961 Apr 21 '14 at 04:49
  • @Vidia Which jQuery version are you using? The result isn't wrong. When an element is hidden, the height is in fact 0. What are you trying to achieve here? – josephting Apr 21 '14 at 04:51
  • @josephting i'm using jquery 1.10 , if the element height is 0 for hidden objects , why did the same work in FF and chrome ? i dont get whats wrong with IE – Vidia Apr 21 '14 at 04:53
  • @Vidia Did you mean FF and Chrome returned the correct height? When did you run this code - `var value = obj.prop("height");`? `$(document).ready()`? `$(window).load()`? – josephting Apr 21 '14 at 04:54
  • Did you use js code to hide the image tag or just set it in html code? – OQJF Apr 21 '14 at 05:14