I have a DOM that is being set (in part) by a plugin at runtime so I don't have full control over it. The initial object that I create in my HTML is a <select>
object to which the plugin bootstrap-select
is creating a <button>
as a sibling. This <button>
in turn has a child <span>
element (also created by the plugin). It is this <span>
element whose text I want to change.
I also am changing the background color of the button itself, and this works. I just need to add a line or two to my function to update the text of the span, and I'm good to go.
Here is a screenshot of the relevant part of the DOM (the gray part is what I have to set):
And here is my code. This was written originally to set the background color of the button, and to that extent it works correctly. I just need to expand it to set the text, as well:
function setColorForSelect(elem, col) {
// this will change the background color of the <select> object just changed to match the selection
// this is necessary because bootstrap-select imposes a complex DOM structure on the <select> item
// (there is a <button> that is what is being displayed, so that is the object whose background color needs
// to be changed, not the <select> object itself)
$(elem).siblings().each(function() {
var _this = $(this);
if(_this.is(":button")){
_this.css('background-color', col);
$(this).children(":first").innerHTML = col; //<-- THIS IS THE LINE THAT DOESN'T WORK!
return false;
}
});
}
This isn't throwing any errors, but it isn't changing anything that I can see. How can I get a reference to this <span>
so that I can change its text? Thank you!