0

I want to know why JQuery .css method returns a default value when no style attribute is provided just like in example here

var r = $('#mmalam').css('display');   $('#result').html(r);

I made a simple div, didn't give it style attribute but still .css method is returning "display = block"

Furthermore is there any JQuery method that will return empty value when no style is provided to the element?

Community
  • 1
  • 1

2 Answers2

2

All browsers have a built in style-sheet which dictates how an element should be displayed. divs are block elements and hence have the default style for display set to block.

This answer should shed some more light.

Community
  • 1
  • 1
Vimal Stan
  • 2,007
  • 1
  • 12
  • 14
  • Good one, can you please also suggest me any method name which will only provide element's style value and if its not given then the function return empty/null. – Muhammad Mutahar Alam Apr 04 '13 at 12:18
  • I'm not aware of any function that will provide what you're asking for. Since all elements have a default style associated with them, you will get some value when you check. What are you trying to achieve? Maybe we can suggest an alternative approach. – Vimal Stan Apr 04 '13 at 12:24
  • what I am trying is to convert the javascript statement "document.getElementById('someid').style.display" to jquery code $('#someid').css('display') while style attribute is given dynamically (and is not provided in this case), however exactly as per your answer browser stylesheet is intervening and .css is ving me display value of "inline" which I thought should be empty when style is not provided – Muhammad Mutahar Alam Apr 04 '13 at 12:58
0

It's because the default display value is block

Eli
  • 14,779
  • 5
  • 59
  • 77