0

I need star rating. In page value are comming from Java/Backend.(I need only javascript or prototypejs solution.)

Eg: If value come 1 then it must show 1 STAR.

If value come 2 then it must show 2 STAR and so on...till 5

This whole thing is happening in dynamic . I am using below code, but this does not create ID.

Javascript

function display() {
   var x = "yr";
    show_image(x ,2) ;
}

function show_image (id,number) {
    var x = number;
    var y = id;
    for (var i =0; i<x; i++){
        var img = document.createElement("img");
        img.src = "stars.png";
       document.getElementById(y).appendChild(img);
    }
}

Thanks for help.

Mike Phils
  • 3,475
  • 5
  • 24
  • 45

1 Answers1

1

Check this fiddle1 fiddle2

//Just for Demo purpose
function getRating() {
    var number = prompt("Enter the rating?");
    if(number *= 1 > 0  && number <=5) {
        show_image('yr', number);
    } else {
        alert("Enter valid rating, greater than 0");
    }
}

function show_image (id,number) {
    document.getElementById(id).innerHTML = '';
    for (var i =0; i<number; i++){
        var img = document.createElement("img");
        img.height=10;
        img.src = "http://icons.iconarchive.com/icons/custom-icon-design/flatastic-2/512/star-full-icon.png";
       document.getElementById(id).appendChild(img);
    }
}

Your code has no issue, it is working fine. Simplest way to call this function from your Java Servlet/JSP response is, added <script>show_image('_some_id_', number_of_star)</script>

Add comment, if you have any query, or if my understanding of problem itself is not correct.

rajuGT
  • 6,224
  • 2
  • 26
  • 44