views:

74

answers:

2

Hello,

I'm using a star rating, 100% working except when i insert values with javascript :

EXAMPLE : http://www.gamer-certified.fr/test.html

    <script type="text/javascript">
jQuery(document).ready(function() { 

jQuery.ajax({
      type: "get",
      dataType: "jsonp",
      url: "http://www.gamer-certified.fr/export/widget.php",
      data: {demandeur: "esl" },
      cache: true, 
      success: function(data, textStatus, XMLHttpRequest){

        var obj = null, length = data.length;
        for (var i = 0; i < length; i++) {

            widget = "<span class='stars'>"+(data[i].qualite / 2)+"</span>" // automatic not working
            widget = "<span class='stars'>4</span>" // manually not working
            jQuery('#gamer-certified'+i).html(widget);
            }    
    } 
});
});
</script>

The only thing is working is putting directly the data in the span value OUTSIDE the widget +=

<span class="stars">4</span> // is working when directly put on the HTML side

EXAMPLE : http://www.gamer-certified.fr/test.html

Thanks !

+1  A: 

To find out why manually entered data works and the data which you receive does not, you need to figure out what is different between the manual data and the entered data. Is it the timing of a jQuery event? Is it the formatting of the data? Once you find this difference, make sure that you address it using the correct means.

EDIT: based on a review of the code, the $.stars() call happens on $(document).ready(), while the content is retrieved after that. Call `$.stars() in the callback, not before.

justkt
i just updated the post, and I explain a lot lot lot much better !
Tristan
@Tristan - have you tried wrapping the calculation for the stars in ()?
justkt
I just updated it with () aroud the calculation : no effects
Tristan
And you've done an `alert` or (with Firebug) `console.log()` to make sure the values are numeric and what you expect?
justkt
yeah the value are numeric, look at : http://www.gamer-certified.fr/test.html the first value is from the ajax request. It's a numeric value. The second one, i typed manually 4. So this is a numeric value too, but no stars ='(
Tristan
You need to make sure the `span.stars` call happens AFTER the content is inserted into the DOM, as you expected.
justkt
omg, you're a god, thank you very much !
Tristan
+1  A: 

I don't know if this will sove your problem, but there's a + missing before 0:

widget = "<span class='stars'>"+0+(data[i].qualite / 2)+"</span>"

j.
i'm sorry, it's not solving my problem, it was a mistake i did when i update the post
Tristan
Good catch, though!
justkt