I have css classes such as .blue, .red etc that define background colors. I want to add specific class to div containing class 'fancybox-title', depending on the id that belongs to the fancybox
<div class="fancybox-skin">
<div class="fancybox-title fancybox-title-inside-wrap">Welcome</div> //here I want to add class, for example '.blue'...
<div class="fancybox-outer">
<div class="fancybox-inner">
<div style="display: block;" id="login-register-fancybox"></div> //depending on the ID from this line of code
</div>
</div>
</div>
I've had several attempts inside of:
$('.fancybox').fancybox({
'beforeLoad': function(){
}
});
including trying to find elements by ID, switches etc, but scripts started to look to big and to messy while not giving me desired result. I believe there is much simpler way to accomplish that, just can't figure it out. Can anyone help?
I'm going to provide you with two solutions:
First: The answer to your question "depending on the id that belongs to the fancybox" uses a switch statement and has the classes hardcoded.
this
contains an attributecontent
: the loaded content. By wrapping the content in a jQuery object we can get the id of the content.Second: To make your code more generic I added a new attribute to your HTML
data-title-class
, this value of this attribute is set added to the classes of the fancybox title.Both solutions use the callback
afterShow
because on earlier callbacks thefancybox-title
is not added yet.