Your needed variable is causing the problem.
needed = $(this).text();
Change from needed = $(this).html(); to needed = $(this).text();
The problem is that on each click on li
you append image again and again, event it's already been checked before. So you need a way to know that this element is already clicked and do nothing in this case.
For example you can add additional CSS class. Something like this (with some additional improvements to the code):
$('ul.gift li').click(function (e) {
if ($(this).hasClass('answered')) {
return;
}
var correct = "gift";
var needed = $(this).html();
if (correct == needed) {
var $innercontent = '<img src="images/right.png">';
$(this).addClass('answered correct').append($innercontent);
} else {
var $innercontent = '<img src="images/wrong.png"></li>';
$(this).addClass('answered wrong').append($innercontent);
}
});
A few notes. 1). Instead of $( e.target ).closest("li")
you can simply use $(this)
as in your case this is the same element. 2). Always declare variables with var
keyword. 3). Instead of inline styles it's more flexible to use classes, e.g. .right {color: green}
instead of style="color: green"
.
$(document).ready(function()
{
$('ul.gift li').click(function(e)
{
correct = "gift";
needed = $(this).html();
if(correct==needed){
$(this).addClass('correct');
}else{
$(this).addClass('incorrect');
}
});
});
ul li {
background-position: right center;
background-repeat: no-repeat;
background-size: 30px 30px;
height: 30px;
width: 100px;
}
.correct {
background-image: url(http://chinagorman.files.wordpress.com/2011/10/check-mark-small.jpg);
color: green;
}
.incorrect {
background-image: url(https://www.catholicmatri.com/images/wrong-icon.png);
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list gift">
<li>gasp</li>
<li>gift</li>
<li>golf</li>
<li>gust</li>
</ul>
Just via CSS with less JavaScript is a bit cleaner
$(document).ready(function () {
$('ul.gift li').click(function () {
var correct = "gift",
given = $(this).text();
$("ul.gift li").removeClass("co in");
if (correct === given) {
$(this).addClass("co");
} else {
$(this).addClass("in");
}
});
});
.co:after{
content:url("http://findicons.com/files/icons/1609/ose_png/256/tick.png");
zoom: .06;
}
.in:after{
content:url("http://findicons.com/files/icons/1014/ivista/128/error.png");
zoom: .1;
}
li:hover{
cursor:pointer;
}
http://jsbin.com/retujikoto/1/edit
I'm using Cors requests to communicate between my client and server placed in two different domain. I configurate my apache http server this way it's using SSL : //with AJAX withCredentials=true (...
I'm using Cors requests to communicate between my client and server placed in two different domain. I configurate my apache http server this way it's using SSL : //with AJAX withCredentials=true (...
Im using dropzonejs to upload photos to my website. The problem is, when I click dropzone to upload, it won't show 'Gallery' as an option as seen in this image : How to add Gallery as an option?
Im using dropzonejs to upload photos to my website. The problem is, when I click dropzone to upload, it won't show 'Gallery' as an option as seen in this image : How to add Gallery as an option?
I am trying to implement a chat application using the codeigniter framework but on form submit, the form data does not get submitted and the submit button becomes disabled. when I check on the ...
I am trying to implement a chat application using the codeigniter framework but on form submit, the form data does not get submitted and the submit button becomes disabled. when I check on the ...
Sincere apologies if this has already been covered. I've been trawling StackOverflow for a while now and I cannot find anything that will fix my issue. Many people have this error, but theirs seem to ...
Sincere apologies if this has already been covered. I've been trawling StackOverflow for a while now and I cannot find anything that will fix my issue. Many people have this error, but theirs seem to ...