McMuffin McMuffin - 26 days ago 7
Javascript Question

How to use variabels togehter with else if

I want to receive some data from this

<div id="past">
<div data-rollid="rollid" class="ball ball-1">"number"</div>
<div data-rollid="rollid" class="ball ball-1">"number"</div>
<div data-rollid="rollid" class="ball ball-8">"number"</div>
<div data-rollid="rollid" class="ball ball-8">"number"</div>
<div data-rollid="rollid" class="ball ball-0">"number"</div>
<div data-rollid="rollid" class="ball ball-8">"number"</div>
<div data-rollid="rollid" class="ball ball-1">"number"</div>
<div data-rollid="rollid" class="ball ball-1">"number"</div>
<div data-rollid="rollid" class="ball ball-8">"number"</div>
<div data-rollid="rollid" class="ball ball-1">"number"</div>
</div>


I only want to receive the number from the last div, for that i use

$('#past')[0].childNodes[9].textContent;


then i want different things to happen when a specefic number is in that div

(function(){

var roll_history = $('#past')[0].childNodes[9].textContent;

if (roll_history = 0) {
console.log('green');
}
else if (roll_history = 1) {
console.log('red');
}
else if (roll_history = 2) {
console.log('red');
}
else if (roll_history = 3) {
console.log('red');
}
else if (roll_history = 4) {
console.log('red');
}
else if (roll_history = 5) {
console.log('red');
}
else if (roll_history = 6) {
console.log('red');
}
else if (roll_history = 7) {
console.log('red');
}
else if (roll_history = 8) {
console.log('black');
}
else if (roll_history = 9) {
console.log('black');
}
else if (roll_history = 10) {
console.log('black');
}
else if (roll_history = 11) {
console.log('black');
}
else if (roll_history = 12) {
console.log('black');
}
else if (roll_history = 13) {
console.log('black');
}
else if (roll_history = 14) {
console.log('black');
}
setTimeout(arguments.callee, 35000);
})();


But the only output i get is "red"

I might gonna use something else than console.log later on.

Answer

Try this

(function(){

var roll_history =  $('#past')[0].childNodes[9].textContent;

console.log(roll_history)
if (roll_history == 0) {
console.log('green');
}
else if ((roll_history > 1) && (roll_history < 7)) {
console.log('red');
}
else if ((roll_history > 8) && (roll_history < 14)) {
console.log('black');
}
setTimeout(arguments.callee, 35000);
})();