0

I am looping out my posts from the database and i am trying to change color of the votes depending on value. If 0 = Black, < 0 = Red, > 0 = Green. My first attemp was to set an id on the h1 tag for the vote value and modify the color from that way. It worked for the first post but all other posts are not affected. Does anyone have any other idea of how i could do this?

PHP & HTML:

while ($row = $stmt->fetch()) {
echo '<h1 id="scoreCounter">'.$row['votes'].'</h1>';
}

Javascript:

var score = document.getElementByid("scoreCounter");
var scoreValue = 10;
checkScore();



function checkScore() {
if (scoreValue < 0) {
  score.style.color = "#FF586C";
} else if (scoreValue > 0) {
  score.style.color = "#6CC576";
} else {
  score.style.color = "#666666";
}
}
2
  • 2
    IDs must be unique. Your PHP loop duplicates them if there's more than one row. Commented Dec 29, 2017 at 18:22
  • 2
    "It worked for the first post but all other posts are not affected" - Use a class. Commented Dec 29, 2017 at 18:23

3 Answers 3

6

A way would be to add an class to the heading, depending on the color to be set.

while ($row = $stmt->fetch()) {
    $class = $row['votes'] == 0 ? 'zero' : ($row['votes'] < 0 ? 'neg' : 'pos');
    echo '<h1 id="scoreCounter" class="' . $class . '">'.$row['votes'].'</h1>';
}

and define css styles for this

h1.zero { color: #000; }
h1.pos { color: #0F0; }
h1.neg { color: #F00; }
Sign up to request clarification or add additional context in comments.

1 Comment

Side note: On the rare instance that this should fail for some reason, adding !important to the rules would help override any previously set rules if a stylesheet such as bootstrap were used.
0

ids should be unique in HTML. Use class instead and getElementsByClassName!

1 Comment

I answered almost immediately after it was posted. Such things can happen.
0

You should never create more than one element with the same ID.

Here is a chunk of code that finds all of the elements with a class name of 'score-counter' and then enumerates though all of them and changes the text color based on the value in the score attribute.

var scoreEls = document.querySelectorAll(".score-counter");
if (scoreEls.length>0) {
  scoreEls.forEach(checkScore);
}

function checkScore(el) {
  var scoreValue = Number(el.getAttribute('score'));
  if (scoreValue < 0) {
    el.style.color = "#FF586C";
  }
  else if (scoreValue > 0) {
    el.style.color = "#6CC576";
  }
  else {
    el.style.color = "#666666";
  }
}
<div>Title 1: <span class="score-counter" score="30">30</span></div>
<div>Title 2: <span class="score-counter" score="0">0</span></div>
<div>Title 3: <span class="score-counter" score="-5">-5</span></div>
<div>Title 4: <span class="score-counter" score="12">12</span></div>
<div>Title 5: <span class="score-counter" score="-53">-53</span></div>
<div>Title 4: <span class="score-counter" score="0">0</span></div>

Your PHP would need to be something like this:

while ($row = $stmt->fetch()) {
  echo '<div>Title 4: <span class="score-counter" score="'.$row['votes'].'">'.$row['votes'].'</span></div>';
}

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.