0

I have a voting script with an arrow system (upvotes and downvotes).If user clicks upvote, the arrow changes to the green arrow, meaning vote registered. If they click again, I want the arrow to revert back to the original image. However, using my code, it changes the image on the first like, but doesn't revert back on a second click.

if (like.src = 'vote_triangle.png') {
  like.src = 'vote_triangle_like.png';
} else {
  like.src = 'vote_triangle.png';
}
2
  • 2
    Well, first of all, you want like.src == 'vote_triangle.png'. Note the ==. Otherwise, you're assigning like.src in the if. Commented Dec 27, 2011 at 4:46
  • Worth noting that the above comment is the correct answer. Commented Dec 27, 2011 at 5:25

3 Answers 3

2

Use a more lenient if statement like:

if (like.src.indexOf('vote_triangle.png')!=-1) {
  like.src = 'vote_triangle_like.png';
} else {
  like.src = 'vote_triangle.png';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey @techfoobar, do you have an idea why if(element.src == "img_path") {} would not work?
If img_path is specified fully (i.e. exactly as it is in element.src, the full case-sensitive path to the url of the image), it will definitely work.
2

I know it's a very old thread, but I would like to add my findings here for future reference.

I found the following code wasn't working:

function swapImage() {
    var element = document.getElementById("myImage");        

    if (element.src == "image1.png") {
        element.src = "image2.png";
    } else {
        element.src = "image1.png"
    }
}

Showing alerts containing the element.src taught me it contained the full path to the image in my local machine. Thus, the if statement had been always evaluated to false.

To fix that in a logical manner, what I did was get the attribute of the element, as the following code shows.

function swapImage() {
    var element = document.getElementById("myImage");

    if (element.getAttribute("src") == "image1.png") {
        element.src = "image2.png";
    } else {
        element.src = "image1.png";
    }
}

By using the function getAttribute("attributeName"), I was able to retrieve the path contained in the src relatively to the project directory.

Comments

1

I would suggest, instead of using img soruce as conditional statement, use a global variable, change its state once the upvote is clicked by say +1 and for downvotes -1.

//when 0, show upvote image, make it a global by declaring before any function
var UpVote = 0;

//when upvote clicked, when greater than 0, show down vote img
UpVote = UpVote +1 ;

//conditional logic for img source
if(UpVote > 0){
  like.src = 'vote_triangle.png';
}
else{
  like.src = 'vote_triangle_like.png';
}

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.