Jump to content

Archived

This topic is now archived and is closed to further replies.

tomfmason

determine display state??

Recommended Posts

I have been beating my head against a wall on this one... I have tired the following

[code=php:0]
function searchBox() {
    var doc = document.getElementById('search').style.display;
    if (doc == 'none') {
        doc = 'block';
    } else if (doc == 'block') {
        doc = 'none';
    } else {
        alert(doc);
    }
}
[/code]

This always alerts undefined.

I also tried adding a .value and got the same result.

Any suggestions?

Thanks,
Tom

Share this post


Link to post
Share on other sites
I just thought of something... If I can't get it to work like this then I could always add a hidden input field and then update the value depending on the display state.. like this.

[code=php:0]
function search() {
    var field = document.getElementById('field').value;
    var doc = document.getElementById('search').style.display;
    if (field == 'none') {
        doc = 'block';
        field = 'block';
    }else if (field == 'none') {
        doc = 'none';
        field = 'none';
    } else {
        alert('There was an error');
    }
}
[/code]

Share this post


Link to post
Share on other sites
That works but it is not the way that I want to do it. Can any one give me a reason that the variable doc will not return a value...?

Share this post


Link to post
Share on other sites
Can you just define it first? Like in your css put in display:block, and then it would be block? If you don't ever define it, it doesn't have a value, hence undefined.

I agree with you that it seems logical that it should have an initial value if you don't explicitly set it in CSS (i.e. should be block (or actually inline should be the default, IIRC)). It just seems that it's not the case, so you'll have to modify your code to assume that undefined == 'block' or 'inline'.

Share this post


Link to post
Share on other sites
It returns nothing ether way. If I have it set to block or none.. I know that there has to be a way to do it..

Thanks,
Tom

Share this post


Link to post
Share on other sites
LOL.. I figured it out. I had the display set in the CSS.. For this I have to set it in the tag it's self..

Thanks,
Tom

Share this post


Link to post
Share on other sites
Also, some browsers won't specify a default, but rather tell you what was explicitly requested.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.