0
<style type="text/css">
            .RadWindow .rwWindowContent .radalert
            {
                background-image:url("images/InfoImg.png"); 
            }
        </style>

The above code in css file ,i want change the background image of the above css file, when user call the Jquery costume function,I want to Change the background image of rad alert.

1
  • On a related note, three class selectors ? That could slow down page rendering. Commented May 17, 2013 at 12:59

4 Answers 4

4

Use two classes :

<style type="text/css">
    .RadWindow .rwWindowContent .radalert {
         background-image:url("images/InfoImg.png"); 
    }
    .RadWindow.newbg .rwWindowContent .radalert {
         background-image:url("images/other_image.png"); 
    }
</style>

JS:

$('.RadWindow').addClass('newbg');    // adds the new background
$('.RadWindow').removeClass('newbg'); // removes the new background
$('.RadWindow').toggleClass('newbg'); // toggles the new background
Sign up to request clarification or add additional context in comments.

1 Comment

great answer, you should really mention jQuery's .toggleClass() ability
2

let suppose you have to change the url of the image to "images/InfoImg2.png" you can use the code option 1-

jQuery -

var newUrl = "images/InfoImg2.png";
$(".radalert").css({
    backgroundImage : 'url("'+newUrl+'")'
});

OR

CSS -

RadWindow .rwWindowContent .radalert2{
  background-image:url("images/InfoImg2.png"); 
}

jQuery -

$(".radalert").addClass("radalert2");

1 Comment

these are all good answers, though i have to go with the 2 class idead myself, as best suggestion
1

Try

$('.RadWindow .rwWindowContent .radalert').css('background-image', 'images/new.png')

1 Comment

lol, you're late to the party, but here's a point anyway
1

Try this simple old fashioned method:

$(document).ready(function(){
    $('.RadWindow .rwWindowContent .radalert')
    .css('background-image', "url('images/InfoImg.png')");
})

OR

as per comment request, you could make use of jQuery's .toggleClass() method. To make best advantage, simple set 2 alternating background classes in css. Assign one to your element on load (write it in the HTML) and simply have the other one ready. Then, on an "action", use toggleClass to alternate the two classes!

Working Example

How it works is simple. First set up your CSS:

<style type="text/css">
    .bg-type-01 {
        background-image: url("some url");
    }
    .bg-type-02 {
        background-image: url("another url");
    }
</style>

Then apply your first class to your element:

<div class="radalert bg-type-01"> ... </div>

Finally, write the very simple JS with jQuery!

<script type="text/javascript">
    $(function() {
        $("something").on("click", function(e) {
            $(".radalert").toggleClass("bg-type-01 bg-type-02");
        });
    })
</script>

6 Comments

all good answers, but i gota point out, having 2 classes gives the editor the "toggle" ability without a bunch of extra code
The selector is not the same as the OP's one : there should be space between classes (ancestor selector) instead of commas (other selector)...
I thought that they are diff classes,sorry for that,I have edited those,Thanx
@Gautam3164 ok right...
Can you consider the toggleClass also..??
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.