0

I know how to change classes with Jquery when the window size is changed, but I need it to be based on the width of a DIV and change dynamically when the DIV's width changes.

$(window).resize(function() {

   var wrapWidth = $('.info-wrap').width();

    if (wrapWidth >= 500) {

      $('#partOne').addClass('big');
      $('#partTwo').addClass('big');

    } else {

      $('#partOne').removeClass('big');
      $('#partTwo').removeClass('big');
    }
});

This works when the window size changes. But, what can I use insead of $(window).resize to get the width of the DIV as it changes?

9
  • 1
    What triggers the resizing of the div? Commented Jul 17, 2013 at 20:42
  • If DIV's width changes only on window resize, what is the issue here? Commented Jul 17, 2013 at 20:42
  • 1
    What's calling the resize? An existing plugin? or does it have CSS3 resize:both enabled? If it is bound by CSS3, check here -> How to detect CSS3 resize events Commented Jul 17, 2013 at 20:43
  • stackoverflow.com/questions/6492683/… Commented Jul 17, 2013 at 20:45
  • Could not help myself: you can rewrite this as $('#partOne, #partTwo').toggleClass('big', wrapWidth >= 500) Commented Jul 17, 2013 at 20:55

4 Answers 4

3

Here is an example of observing the elements attributes.

See: MutationObserver and Mutation Events

CSS

#watch {
    border: 1px solid;
}

HTML

<button id="button">Click me</button>
<div id="watch" style="width: 100px; height: 50px;"></div>

Javascript

/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/* global global */

(function (global) {
    "use strict";

    if (typeof global.MutationObserver !== "function") {
        global.MutationObserver = global.WebKitMutationObserver || global.MozMutationObserver;
    }

    var watch = document.getElementById("watch");

    function whenClicked() {
        watch.style.width = "200px";
    }

    document.getElementById("button").addEventListener("click", whenClicked, false);

    if (typeof global.MutationObserver !== "function") {
        // chrome doesn't despatch an event for "DOMAttrModified"
        watch.addEventListener("DOMAttrModified", function (evt) {
            console.log("Attribute changed", evt.target);
        }, false);
    } else {
        var observer = new global.MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                if (mutation.type === 'attributes') {
                    console.log("Attribute changed", mutation);
                }
            });
        });

        observer.observe(watch, {
            attributes: true,
            childList: true,
            characterData: true,
            subtree: true
        });
    }
}(window));

On jsfiddle

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

Comments

2

I wrote a plugin sometime back for attrchange listener which basically adds a listener function on attribute change. This seems to come in handy for scenario that you had mentioned where you need a handler to check the width and height.

DEMO: http://jsfiddle.net/CKTk3/1/

    var prevWidth = $('#test').width(),
        prevHeight = $('#test').height();

    $('#test').attrchange({
        callback: function (e) {
            var curWidth = $(this).width(),
                curHeight = $(this).height();            
            if (prevWidth !== curWidth ||
                prevHeight !== curHeight) {
                console.log('resized: width - ' + prevWidth + ' : ' + curWidth + ' height - ' + prevHeight + ' : ' + curHeight);

                prevWidth = curWidth;
                prevHeight = curHeight;
            }            
        }
    }).resizable();

Plugin page: http://meetselva.github.io/attrchange/

2 Comments

+1, like the tracking fix for MutationObserver. Much better than the polling solution.
@Xotic750 Thanks for checking out my answer.
0

I think you'll need to use a plugin for JQuery, such Ben Alman's plugin.

I don't think there exists something that detects when a div gets resize, only the window.

1 Comment

Note: This plugin uses polling.
0

You can get div width by div class or id. ex: $("#div-id").width();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.