I'm new to jquery.
I'm trying to make a simple drag and drop game. The html looks like this
<div class="set-text">
<div class="text">cinema</div>
<div class="text">post-office</div>
</div>
<div class="set-image-box">
<img class="image" src="images/cinema.png" alt="cinema">
<div class="box"></div>
</div>
<div class="set-image-box">
<img class="image" src="images/post-office.png" alt="post-office">
<div class="box"></div>
</div>
- I want to make the .text draggable to the .box
- and then on drop I need to compare the .text text with the alt atribute of the image.
So far I have this code for that:
$(document).ready(function() {
var txt = $(".text").draggable({ revert: "invalid" });
var Box = $(".box").droppable({activeClass: "highlight",
drop: function( event, ui ){
$( this ).addClass("dropped");
}
});
var draggedText = $(".text").mouseup( getText );
function getText (){
var t = $(this).text();
}
var imageName = $(txt).on( "dragstop", function( event, ui ) {
var imgName = $(this).siblings(".image").find("img").attr("alt");
alert(imgName);
});
if (draggedText == imageName){
$(this).addClass("correct");
}else{
$(this).addClass("incorrect");
}
});
I'm stuck with the getting var imageName value which should be the value of the alt tag of the image placed in this very set. alert returns me Undefined. I have searched through this forum and google but cannot find anything that solves my problem.
I think that I also miss something in the code to define where the text was dropped, in which sibling should the function look for....
I would really appreciate your help! Thanks in advance.