pjldesign
pjldesign

Reputation: 397

Add class to dynamically changing element

I'm trying to use the attrchange plugin to listen to a change in one element and add a class to another element. Kind of novice to jquery and javascript and could use some help with this...

function navchange() {
    document.getElementByClassName("sticky-element-cloned").addClass('shrinkwrap');
}

$(".sticky-element-original").attrchange({
  trackValues: true, 
  callback: function (event) {
    //event.attributeName - Attribute Name
    //event.oldValue - Prev Value
    //event.newValue - New Value
    navchange();
    }
});

Thanks for any insight.

Upvotes: 1

Views: 49

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44145

Two things. Firstly, fix your DOM method to make it valid.

document.getElementsByClassName(".sticky-element-cloned")[0].classList.add("shrinkwrap");

Secondly - isn't the class sticky-element-cloned? Then why are you using sticky-element-original in your jQuery?

$(".sticky-element-cloned").attrChange({...});

Or (do one or the other, not both)

Change it in your JavaScript:

document.getElementsByClassName(".sticky-element-original")[0].classList.add("shrinkwrap");

Upvotes: 1

Chris HG
Chris HG

Reputation: 1492

On the navchange function you're mixing your pure Javascript with your jQuery. Try:

$(".sticky-element-cloned").addClass("shrinkwrap");

Upvotes: 0

Related Questions