1

I'm trying to make a ratings system but I'm at a stand with the javascript. I have made a method which is suppose to swap out the image of a blank star for a star.

function setStar(intStarId) {
var stars = new Array("star_one","star_two","star_three","star_four","star_five");
for (var i = 0; i < intStarId; i++)
    document.getElementById(stars[i]).src = "images/star.png";
}

When I call this method it does not do anything with the star. How could I get this working so it swaps the blank star image for the star image?

Here is how I'm calling it in html. (it's actually php but it's echoing html)

echo "<script language='javascript'>setStar(3)</script>";

And here is the html for the stars (php echoing html)

echo "<ul name='a' style='list-style: none; margin: 0px; float: right;'>
                <li name='b' align='center'>
                    <form name = 'test' action='#' method='post'>
                        <a href='#'><img name='star_one' src='images/star_blank.png' onmouseover='hoverInStar(1)' onmouseout='hoverOutStar(1)'/></a>
                        <a href='#'><img name='star_two' src='images/star_blank.png' onmouseover='hoverInStar(2)' onmouseout='hoverOutStar(2)'/></a>
                        <a href='#'><img name='star_three' src='images/star_blank.png' onmouseover='hoverInStar(3)' onmouseout='hoverOutStar(3)'/></a>
                        <a href='#'><img name='star_four' src='images/star_blank.png' onmouseover='hoverInStar(4)' onmouseout='hoverOutStar(4)'/></a>
                        <a href='#'><img name='star_five' src='images/star_blank.png' onmouseover='hoverInStar(5)' onmouseout='hoverOutStar(5)'/></a>
                    </form>
                </li>
            </ul>";

3 Answers 3

2

You are trying to find the element in DOM by using the id. But your HTML output contains only name. Try modifying you code to below

echo "<ul name='a' style='list-style: none; margin: 0px; float: right;'>
                <li name='b' align='center'>
                    <form name = 'test' action='#' method='post'>
                        <a href='#'><img id='star_one' src='images/star_blank.png' onmouseover='hoverInStar(1)' onmouseout='hoverOutStar(1)'/></a>
                        <a href='#'><img id='star_two' src='images/star_blank.png' onmouseover='hoverInStar(2)' onmouseout='hoverOutStar(2)'/></a>
                        <a href='#'><img id='star_three' src='images/star_blank.png' onmouseover='hoverInStar(3)' onmouseout='hoverOutStar(3)'/></a>
                        <a href='#'><img id='star_four' src='images/star_blank.png' onmouseover='hoverInStar(4)' onmouseout='hoverOutStar(4)'/></a>
                        <a href='#'><img id='star_five' src='images/star_blank.png' onmouseover='hoverInStar(5)' onmouseout='hoverOutStar(5)'/></a>
                    </form>
                </li>
            </ul>";

Please also note, id should be unique for all the elements in the DOM.

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

1 Comment

I didn't even notice. Thank you!
1

I think you should use getElement*sByName* instead getElement*ById* ;-) Pay attention, because getElementsByName doesn't return an element, but a node list:

function setStar(intStarId) {
    var stars = new Array("star_one","star_two","star_three","star_four","star_five");
    for (var i = 0; i < intStarId; i++) {
        document.getElementsByName(stars[i])[0].src = "images/star.png";

    }
}

1 Comment

or instead replace name with id in you markup as posed by Ramesh :-)
1

You should just use jQuery, with the following Library, it would have been easier :)

Have a look at http://www.jquery.com/ and http://plugins.learningjquery.com/half-star-rating/

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.