DIM3NSION
DIM3NSION

Reputation: 1761

Javascript to change css

How would I go about creating a script that checked if a div was set to any other value than display:none.

I have an image over an accordion, but when I click on an accordion element I want to hide the image over the top.

So say my content was in the form of a class called "accord_content" and my image that is displayed was "image_over". Could i create a script that checked to see if accord_content was shown and if so to hide "image_over"?

if ('.accord_content.display !== 'none') {
        image_over.display = 'none')
        }

Just a quick try, not to good at javascript so confused at how to complete this

Thanks

New update - This is an extract of the html that i want to check is displayed

<h3 class="title">Making the shortlist</h3>
        <div class="accord_content">
        <div class="content_left">

<p>CONTENT</p>

<p>CONTENT>
    </div>

Now this is using the jquery accordion ui, so when the page is open class accord_content is display:none,

and when the the h3 is clicked it changes to display:block.

thanks

Upvotes: 0

Views: 123

Answers (2)

3dgoo
3dgoo

Reputation: 15804

Using jQuery you can do the following:

Javascript

if ($('.accord_content').css('display') !== 'none') {
    $('.image_over').hide();
}

Demo

This will work in the following way. If any .accord_content is visible then all .image_over are hidden.

Or in native javascript you can do this:

Javascript

if (document.querySelector('.accord_content').style.display !== 'none') {
    document.querySelector('.image_over').style.display = 'none';
}

Demo

This will get the first instance of .accord_content and hide the first .image_over. This check will only return false if display: none is set to .accord_content as an inline style attribute and not with css.

You should really post some relevant html in your question. Also a description of how this is to work. If you post a vague question you will get a vague answer. Take the time to post a good question and people will take the time to post a good answer.

Upvotes: 2

Brian Cray
Brian Cray

Reputation: 1275

if (element.style.display !== 'none') {}

Upvotes: 0

Related Questions