am trying to develop a quiz snippet with drag and drop and end up with this code where its always showing result as 1 the result is not incrementing…..
i tried many ways but its not incrementing…..
javascript:
here is my code which i used in the
<script>
function allowDropi(ev)
{
ev.preventDefault();
}
function dragi(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}
function dropi(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
result1=0;
if(ev.dataTransfer.getData("Text")==="drag2i")
{
result1++;
}
if(ev.dataTransfer.getData("Text")==="dra2i")
{
result1++;
}
if(ev.dataTransfer.getData("Text")==="dr2i")
{
result1++;
}
if(ev.dataTransfer.getData("Text")==="2i")
{
result1++;
}
if(ev.dataTransfer.getData("Text")==="d2i")
{
result1++;
}
document.getElementById('boldStuff6').innerHTML = result1;
}
alert(result1);
</script>
where am i going wrong please check mycode and help me in getting the result…
The reason is that everytime you call this function result is being set to 0 by “result1=0” and then you are incremeting it afterwards.
If you want to keep count of the result you will neet to make result as a property of the function.
instead of “result1=0” add:
and change your incrementers to the following:
I hope that answers your question.
thanks,
Miter