Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RemoveClass AddClass When Page Load [duplicate]

Tags:

html

jquery

css

I have several <div> tags with different class names. When the page loads, I want to remove and add another class in a certain <div>, but somehow it doesn't work as expected.

LIVE CODE

HTML

<div class="redBG">
    <div class="yellowIcon"></div>
</div>   
<div class="greenBG">
    <div class="yellowIcon"></div>
</div> 

CSS

.redBG{
    background-color:red;
    width: 300px;
    height:30px;
    z-index: 1;
}
.yellowIcon{
    background-color:yellow;
    width: 20px;
    height:20px;
    cursor: pointer;
    z-index:20;
}
.greenBG{background-color:green}

.black{background-color:black}

JS

$('div, .redBG').load(
    function()
    {
        $(this).removeClass('.redBG').addClass('.black');
    }
);
like image 231
abcid d Avatar asked Jun 25 '26 16:06

abcid d


1 Answers

In jQuery, .load is for loading data from a server and inserting it into an element: http://api.jquery.com/load/

Here's jQuery for the page load and changing the classes:

$(document).ready(function() {
  $('.redBG').removeClass('redBG').addClass('black');
});

Also, don't use a period for the class when using removeClass and addClass.

JSFiddle: http://jsfiddle.net/15jg5hbn/

like image 190
Darren Avatar answered Jun 28 '26 04:06

Darren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!