I'm building a simple voting thingy that'll appear in the sidebar of a college's website. The way it work's is simple. You pick who you like and that's it. It's structured like below. There's a heading
, sub-heading
and then candidates
. Next to each candidate
is the like
link.
I'm stuck here: When a user clicks a like
link, if what happens in demo.php
is successful, then all the other like
links for that sub-heading
need to be taken out, so the user cannot vote for anyone else under that sub-heading
anymore.
How can something like this be done when it's all built this way. If the </div>
of id=h2
were moved below all the like
, it'll make thing easier I feel.
I'm willing to implement changes since this is just being built.
Here's my demo.htm
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Side bar voting thingy</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">
$(function() {
$(".like").click(function() {
var hasLike = $(this).data("id");
var data = 'id='+hasLike;
console.log($(this).data('id'));
if(hasLike) {
// ajax call
$.ajax({
type:"GET",
url:"demo.php",
data:data,
beforeSend:function(html){
// We'll think of something to do here
},
success: function(page_data){
// Remove the remaining like links. How?
$('.like[data-id="'+page_data+'"]').append(page_data);
},
error: function(page_data){
$("#errors").empty();
$("#errors").fadeIn(200);
$("#errors").append('Screwed up!');
},
});
}
return false;
});
});
</script>
</head>
<body>
<div id="container">
<div id="h1" data-id="1">Teachers</div>
<div id="h2" data-id="2">Who is your favorite Math teacher?</div>
<div>* Homer Simpson   <span id="h3" class="like" data-id="3" data-sec="2">Like</span></div>
<div>* Elmer Fudd   <span id="h3" class="like" data-id="4" data-sec="2">Like</span></div>
<div>* Bugs Bunny   <span id="h3" class="like" data-id="5" data-sec="2">Like</span></div>
<div>* Obelix   <span id="h3" class="like" data-id="6" data-sec="2">Like</span></div>
<div>* Mojo Jojo   <span id="h3" class="like" data-id="7" data-sec="2">Like</span></div>
<br>
<div id="h1" data-id="8">Restaurants</div>
<div id="h2" data-id="9">Which is your favourtie restaurant in town?</div>
<div>* McDonalds   <span id="h3" class="like" data-id="10" data-sec="9">Like</span></div>
<div>* KFC   <span id="h3" class="like" data-id="11" data-sec="9">Like</span></div>
<div>* The Heart Attack Grill   <span id="h3" class="like" data-id="12" data-sec="9">Like</span></div>
<div>* In-n-Out   <span id="h3" class="like" data-id="13" data-sec="9">Like</span></div>
<div>* Popeye's   <span id="h3" class="like" data-id="14" data-sec="9">Like</span></div>
<div id="errors" style="display:none;"></div>
</div>
</body>
</html>
Here's demo.php (nothing much in here for now)
<?php
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo 'Error! Id not found';
}
?>