I have 10 cards with a "yes" and "no" button on each of them. I need to make so that people can select a yes or a no on each of them. Whenever you make a "yes" selection it's background would turn into green, but if you select "no" the "yes" would turn off and the "no" would turn red. I need to make it so that you can do it on each card without affecting the other one. This means that if I select yes on the first one and then select "no" on the second one, the "yes" on the first one should still stay selected. I found a script that does this however it affects the selection from the other cards.
5 Answers
It's look like you didn't reference to the jquery library $ is not defined
$(document).ready(function () {
$('input[type="button"]').click(function() {
$('input[type="button"]').attr('class', '');
if ($(this).val().toLowerCase() == 'yes') {
$(this).attr('class', 'turnGreen');
}
else {
$(this).attr('class', 'turnRed');
}
});
});
.turnRed {
background-color: red;
}
.turnGreen {
background-color: green;
}
.turnDefault {
background-color: #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card1">
<input type="button" id="yes" value="YES"/>
<input type="button" id="no" value="NO"/>
</div>
<div class="card2">
<input type="button" id="yes" value="YES"/>
<input type="button" id="no" value="NO"/>
</div>
<div class="card3">
<input type="button" id="yes" value="YES"/>
<input type="button" id="no" value="NO"/>
</div>
<div class="card4">
<input type="button" id="yes" value="YES"/>
<input type="button" id="no" value="NO"/>
</div>
4 Comments
Grigori
I did reference in the original document but that's not the issue. This function just does not do the right thing.
mplungjan
I suggest you do not use the latest jQuery in case someone is using IE
mplungjan
Also you remove the complete class?
Mosh Feu
In this case I do, because there is no reason to not do this.
I believe this is the effect you are looking for.
$(document).ready(function () {
$('input[type="button"]').click(function(){
$(this).addClass('active');
$(this).siblings().removeClass('active');
});
});
.card input.active:nth-of-type(1) {
background: green;
}
.card input.active:nth-of-type(2) {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card">
<input type="button" value="YES"/>
<input type="button" value="NO"/>
</div>
<div class="card">
<input type="button" value="YES"/>
<input type="button" value="NO"/>
</div>
<div class="card">
<input type="button" value="YES"/>
<input type="button" value="NO"/>
</div>
<div class="card">
<input type="button" value="YES"/>
<input type="button" value="NO"/>
</div>
Like this?
$(function () {
$('input[type="button"]').click(function () {
var idx = this.id.split("_")[1];
if (this.value=="NO") {
$("#yes_"+idx).removeClass('turnGreen');
$(this).addClass('turnRed');
}
else {
$("#no_"+idx).removeClass('turnRed');
$(this).addClass('turnGreen');
}
});
});
.turnRed {
background-color: red;
}
.turnGreen {
background-color: green;
}
.turnDefault {
background-color: #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="card1">
<input type="button" id="yes_0" value="YES" />
<input type="button" id="no_0" value="NO" />
</div>
<div class="card2">
<input type="button" id="yes_1" value="YES" />
<input type="button" id="no_1" value="NO" />
</div>
<div class="card3">
<input type="button" id="yes_2" value="YES" />
<input type="button" id="no_2" value="NO" />
</div>
<div class="card4">
<input type="button" id="yes_3" value="YES" />
<input type="button" id="no_3" value="NO" />
</div>
Take a look at Working fiddle (Tested in Mozzila & Chrome & Internet Explorer).
You have to change yes/no ids by classes because id should be unique in current document :
HTML :
<div class="card1">
<input type="button" class="yes" value="YES"/>
<input type="button" class="no" value="NO"/>
</div>
<div class="card2">
<input type="button" class="yes" value="YES"/>
<input type="button" class="no" value="NO"/>
</div>
<div class="card3">
<input type="button" class="yes" value="YES"/>
<input type="button" class="no" value="NO"/>
</div>
<div class="card4">
<input type="button" class="yes" value="YES"/>
<input type="button" class="no" value="NO"/>
</div>
JS :
$('input[type="button"]').click(function(){
if($(this).hasClass('no')){
$(this).addClass('turnRed');
$(this).parent().find('.yes').removeClass('turnGreen');
}else{
$(this).addClass('turnGreen');
$(this).parent().find('.no').removeClass('turnRed');
}
});
Hope this helps.
Comments
Just rename ID's, you can't use the same id to more elements http://jsfiddle.net/f4o564j1/
<div class="card1">
<input type="button" id="yes" value="YES"/>
<input type="button" id="no" value="NO"/>
</div>
<div class="card2">
<input type="button" id="yes1" value="YES"/>
<input type="button" id="no1" value="NO"/>
</div>
<div class="card3">
<input type="button" id="yes2" value="YES"/>
<input type="button" id="no2" value="NO"/>
</div>
<div class="card4">
<input type="button" id="yes3" value="YES"/>
<input type="button" id="no3" value="NO"/>
</div>
and reference Jquery!
5 Comments
MozzieMD
Depends in what browser you check! In last chrome it works fine! No reason to down vote!
MozzieMD
Maybe the owner can explain in what browser he trying to make it work!
Grigori
I did not downvote. The thing is that it works but the function it does is different than the result I want to achieve.
MozzieMD
Not you, @Paulie_D, tell me please in what browser you are working?
Paulie_D
I'm using Chrome. The "Yes" button do not turn green. They turn red.
$('input[type="button"]').removeClass('turnRed')