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
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]
Link to comment
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'.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.