views:

71

answers:

2

Why wont this work?

if (byId("annonsera_price").style.display=='inline' || byId("annonsera_price").style.display=='block'){
alert ("Hello");
}

function byId(x){
        return document.getElementById(x);
}

update2:

<input style="width:100px;" type="text" name="annonsera_price" id="annonsera_price">

I haven't set it with css either!

+2  A: 

First try alerting just the style.display and see what it contains:

var el = document.getElementById("annonsera_price");
alert(el.style.display);

style only references what is in the style attribute (or set through JS on the style property). It's possible that the display is set through the class, and therefore el.style.display won't show anything.

[Update]:Given the update, the reason no alert happens is because style.display will be "", not "inline" or "block". It won't be "inline" or "block" until you set it to something, either in the style attribute or by setting .style.display.

Renesis
You can use it in an if statement, that's why I say you need to tell us what it IS returning. If I set up my own element, your code will work for me. We need to find out what the elements contain that you are dealing with.
Renesis
I was wrong, I haven't set it with css at all... check update
Camran
The alerting returns a blank alert-box... No value!
Camran
and it returns a blank alert box when alert(style.display) of the element
Camran
That is correct. `style.display` **will** be blank, until you set it to something. Just because it has a default behavior, doesn't mean the `style.display` holds a default value. A blank value just causes it to use its default behavior.
Renesis
offcourse... Sorry about that stupid mistake! :)
Camran
+2  A: 

If the style is set via CSS or is a default style, you need to get the computed style of the element:

var el    = document.getElementById("annonsera_price");

// currentStyle for IE, getComputedStyle for standards-compliant browsers
var style = el.currentStyle || window.getComputedStyle(el);

if (style.display == "inline" || style.display == "block")
    alert("Hello");
Andy E
That's interesting. I always use tools like FireBug or IEDevtoolbar to find this out but it's always nice to know how to it through code
SBUJOLD