Jump to content

determine display state??


tomfmason

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
Link to comment
https://forums.phpfreaks.com/topic/24728-determine-display-state/
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]
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'.

Archived

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

×
×
  • Create New...

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.