0

I am trying to write a simple conditional that says if div1 is displayed, then change div2's display to none. However, I want this to be updated live. So anytime div1 display is 'grid', div2 disappears from sight.

<script>
    if($('.div1').css("display") == "grid") {
        $('div2').css({"display":"none"});
        }
</script>

What am I doing wrong here?

2
  • The key idea here is to use events that fire whenever something happens. Commented Feb 2, 2015 at 17:44
  • This question might help you out stackoverflow.com/questions/8671001/… Commented Feb 2, 2015 at 17:47

2 Answers 2

2

A block of javascript code will not just magically run whenever convenient for you, unless you make it so that it is run in such a way. What you have written will just run once, and move on. Javascript will not by itself will look for when things change.

You need to track the change and run your code after that change. If you are writing the javascript for a site, you probably know when these changes occur, so you can execute your code block when they do occur. For example if div1 changes to grid when user clicks a button, then you can bind your function to its click event so handle the situation.

A more advanced method would be to watch for changes on DOM and run a function when they occur. You can do this with MutationObservers. You can do precisely what you want, if div changes to grid, run myFunction() for example.

Another method would be to have a function run on intervals but this is an obsolete technique which is prone to errors and crashes and is by no means recommended to be used in javascript.

Sign up to request clarification or add additional context in comments.

Comments

1

The $.watch plugin can do this:

$('.div1').watch('display', function() {
    var display = ($(this).css('display') === 'grid' ? 'none' : 'block');

    $('.div2').css('display', display);
});

Unlike the setInterval method, here's what the library does (from the github page):

This plugin lets you listen for when a CSS property, or properties, changes on element. It utilizes Mutation Observers to mimic the DOMAttrModified (Mutation Events API) and propertychange (Internet Explorer) events.

Be aware that your original code uses $('div2') instead of $('.div2') and will only match elements that look like this: <div2>foo</div2>. I've changed it to a class in my example.

9 Comments

I don't understand this. what does this do exactly?
@Dr.Donnaloia It uses mutation observers if they're available and if they're not it uses IEs propertchange event. It's just an already built wheel made so you don't have to.
so the watch function above is just checking for any property changes? and then if there's a change, (display, display)?
@Dr.Donnaloia correct. Thus is way better than constantly checking.
And in case you're wondering about the ternary operator
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.