Reputation: 1098
On a page I have songs playing in a small audio player and the song change every 1-2 minutes or when I want them to(like a radio). Every song can be voted by users this way:
<span class='up'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="up"><img src="up.png" alt="Down" /></a></span><br />
For upvote button, <div class="totalvotes"></div>
for total votes and
<span class='down'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="down"><img src="down.png" alt="Down" /></a></span>
for downvote button.
Everything is working well but the page obviously will refresh when I click on one of the buttons to vote.
The id="<?php echo $mes_id; ?>"
from a
element is what I need to be updated every 1-2 seconds because it gets the id of the current song playing.
For the totalvotes the solution was using:
function refresh_div() {
jQuery.ajax({
url:'totalvotes.php',
type:'POST',
success:function(totalvotes) {
jQuery(".totalvotes").html(totalvotes);
}
});
}
t = setInterval(refresh_div,1000);
It was easy because I created a div with a class and specified the class in the function. But I have no ideea how can I specify the ID of a "a" to be updated every second.
Upvotes: 1
Views: 1472
Reputation: 1
get the ID of the playing song with Ajax just get the reference to anchor element globally in page load with
var anchor = $("#<?php echo $mes_id; ?>");
and set
$(anchor).attr("id", idReturnedFromAjaxCall);
$(anchor).attr("href", theUrlIBuilded);
Upvotes: 0
Reputation: 1818
You can use jQuery's .attr()
to change any attribute on a HTML element.
function updateLink() {
// get id of current song.
// newValue is that id.
$("a").attr('id',newValue);
}
setInterval(updateLink,1000);
Upvotes: 1
Reputation: 2126
I would rather place that link in a div and update that div with a new link using ajax
Upvotes: 0