0

HTML:

                <div class="rating" id = "r1"></div>
                <div class="rating" id = "r2"></div>
                <div class="rating" id = "r3"></div>
                <div class="rating" id = "r4"></div>
                <div class="rating" id = "r5"></div>

Js (using jquery 1.10.1) :

 <script type = "text/javascript">
                    $(document).ready(function(){
                        var rate = new Array();
                        rate[1] = "r1";
                        rate[2] = "r2";
                        rate[3] = "r3";
                        rate[4] = "r4";
                        rate[5] = "r5";
                        var r = <?php echo $rate;?>
                            for(var i=1; i<=r;i++){
                                var k = rate[i];
                                $('#'+k).css{('background-image': 'url(images/star_green.png)')};

                            }
                    });

                    </script>

Basically what i want to do using this code is to modify the background of the first x divs (number provided in my db). I know that the js variable r takes the right value...same for the k variable...the only thing that i think is not working is the part where the background is set. I tested it using direct values (without the +k part) and it didn't work the either. The page is located in root and the images folder is next to it.

Any suggestions?

PS: The Js script is placed on the page after the divs.

1
  • I think you inversed .css({}) with .css{()} Commented Oct 16, 2013 at 16:20

3 Answers 3

1

Your missing a semicolon here

var r = <?php echo $rate;?>

It should be

var r = <?php echo $rate;?>;

Also

$('#'+k).css{('background-image': 'url(images/star_green.png)')};

should be

$('#'+k).css({'background-image': 'url(images/star_green.png)'});

Sign up to request clarification or add additional context in comments.

1 Comment

There was another mistake. Edited the answer.
0
 $(document).ready(function () {
     var rate = new Array();
     rate[1] = "r1";
     rate[2] = "r2";
     rate[3] = "r3";
     rate[4] = "r4";
     rate[5] = "r5";
     var r = <? php echo $rate; ?> ;
     for (var i = 1; i <= r; i++) {
         var k = rate[i];
         $('#' + k).css
             ({'background-image': 'url(images/star_green.png)'});
         };

     }
 });

Comments

0

Please try this.

$('.rating').css('background-image','url(images/image.jpeg)');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.