I am somehow losing reference to a select element on the page. I'm working with legacy code and I'm trying to rebuild a portion of it. There is other code besides the code I am posting here that changes the value of the select element I am trying to work with.
I create a new object for each visualizer div element on the page. I select all of the select elements with class master_select and store them in masterSelects like this:
masterSelects = wrapper.find( ".master_select" );
Then whenever any select box on the page changes I call updatePreview and it should be able to make changes to the masterSelects.
When the page is first loaded, updatePreview is manually fired and I call:
masterSelects.css('background-color', 'blue')
which works and changes the bg-color of these select boxes as would be expected. However, if I change a select option and updatePreview is called by the event, using this:
masterSelects.css('background-color', 'orange');
fails to update the bg-color. I printed out the masterSelects object to the console log and it still contains the information I would expect. It's like the elements I want to change on the page are no longer the ones I have saved a reference to. However, trying to re-select it also doesn't work. Could the other legacy code somehow be breaking my reference to these elements despite the console log printing out the expected information? I'm sorry I haven't posted that code, it's a 1000 lines and is a mess since I didn't write it, I'm going to start weeding through it, but I wanted to know if this is even possible before I start.
var Visualizer = (function() {
/*********************************
* Internal Variables
*********************************/
var wrapper,
selects,
masterSelects;
/*********************************
* Constructor
*********************************/
function Visualizer( newElement ) {
wrapper = $(newElement);
selects = wrapper.find( "select" );
masterSelects = wrapper.find( ".master_select" );
};
/*********************************
* Internal Methods
*********************************/
var updatePreview = function( isManual ) {
// Default isManual to false if it is not already true
if (isManual != true) isManual = false;
if(isManual) {
masterSelects.css('background-color', 'blue');
console.log(masterSelects);
}
if(!isManual) {
// Even tried reselecting the masterSelects, but it didn't work
// masterSelects = wrapper.find( ".master_select" );
masterSelects.css('background-color', 'orange');
console.log(masterSelects);
}
};
// Bind on change events to the select boxes inside the wrapper div
var bindEvents = function() {
selects.change( updatePreview );
};
/*********************************
* Public Methods
*********************************/
// Initialize the Visualizer object
Visualizer.prototype.init = function() {
bindEvents();
updatePreview( true );
};
// Return the Visualizer object
return Visualizer;
})();
/*********************************
* Usage
*********************************/
$( document ).ready( function() {
$( "div.visualizer-wrapper" ).each( function( key, value ) {
var vis = new Visualizer( value );
vis.init();
});
});
Oh and this code worked on another similar page that has slightly different other legacy code.