Reputation: 5714
I have the following which lets users choose which team they think will win and by how many points
![enter image description here][1]
Based on user selection image-border highlights red for the team user selected.
My problem is, my code only does this for the 1st fixture/match and not the 2nd,3rd,4th etc as you can see in image
Im sure this should be an easy fix, but I just cant seem to get it working...any help would be tremendously appreciated
function border() {
var el = document.getElementsByClassName("t1")[0];
var el2 = document.getElementsByClassName("t2")[0];
if (document.getElementById("one").checked) {
el.style.borderColor = "red";
el.style.borderStyle = "solid";
el2.style.borderColor = "";
el2.style.borderStyle = "none";
}
else if (document.getElementById("two").checked) {
el.style.borderColor = "";
el.style.borderStyle = "none";
el2.style.borderColor = "red";
el2.style.borderStyle = "solid";
}
}
<label class="blue"><input type="radio" onclick="border()" name="picks['.$x.']" id="one" value="'.$row['team1'].'"><span>'.$team1.'</span></label><br />
<label class="green"><input type="radio" onclick="border()" name="picks['.$x.']" id="two" value="'.$row['team2'].'"><span>'.$team2.'</span></label><br />
<label class="green"><input type="radio" onclick="border()" name="picks['.$x.']" id="three" value="draw"><span>Draw</span></label><br />
echo'<div class="teams">';
echo'<img src="images/teams/'.$src1.'.png" class="t1" />';
echo'<img src="images/teams/'.$src2.'.png" class="t2" />';
echo'</div>';
Remember fixtures are generated in a php loop from database depending on how many matches there are in a given round
Upvotes: 0
Views: 69
Reputation: 87203
You need to get all the matching elements and loop over them.
function border() {
var el = document.getElementsByClassName('t1'),
el2 = document.getElementsByClassName('t2');
var borderColor1 = '',
borderStyle1 = 'none',
borderColor2 = '',
borderStyle2 = 'none';
if (document.getElementById('one').checked) {
borderColor1 = 'red';
borderStyle1 = 'solid';
} else if (document.getElementById('two').checked) {
borderColor2 = 'red';
borderStyle2 = 'solid';
}
for(var i = 0; i < el.length; i++) {
el[i].borderColor = borderColor1;
el[i].borderStyle = borderStyle1;
}
for(var j = 0; j < el2.length; j++) {
el2[j].borderColor = borderColor2;
el2[j].borderStyle = borderStyle2;
}
}
jQuery Demo: https://jsfiddle.net/tusharj/dkstwrbf/
Upvotes: 2
Reputation: 28513
Try this : you can loop through all matched element instead of processing only first one
function border() {
var oneChecked = $('#one').is(':checked');
var twoChecked = $('#two').is(':checked');
$('.t1').each(function(){
if(oneChecked)
{
$(this).siblings('t2').css({'border':'none'});
$(this).css({'border':'1px solid red'});
}
else if(twoChecked)
{
$(this).css({'border':'none'});
$(this).siblings('t2').css({'border':'1px solid red'});
}
});
}
Upvotes: 0