I have an ajax function that loads 1 news at time everytime the user clicks on "load more".
It works like a facebook pagination.
The problem is: when it reachs the end of my database, it doesn't load anything else so I would like to remove the link "load more" when it happens but I couldn't do it.
Here's my JS function:
function loadNews(pageLimit){
var dataString = 'pageLimit='+ pageLimit;
$.ajax({
type: "POST",
url: "ajaxnovidades.php",
data: dataString,
cache: false,
success: function(result){
$("#maisnews").hide();
$("#maisupdates").append(result);
}
});
}
loadNews('0');
I have tried to check if result is empty (result=="") and then hide() the #maisnews div, which is the one with the "load more" link but it doesn't work...
Any help?!
Try this:
if ( ! $.trim(result).length ) $('#maisnews').hide();
You can try something like:
function loadNews(pageLimit){
var dataString = 'pageLimit='+ pageLimit;
$.ajax({
type: "POST",
url: "ajaxnovidades.php",
data: dataString,
cache: false,
success: function(result){
if($.trim(result).length==0){
//$(element).hide()
$("#maisnews").hide();
}else{
$("#maisupdates").append(result);
}
}
});
}
loadNews('0');
Related
This code below only works if i click on the button twice and I've got no clue why it does that.
Any hints would be appreciated. guess it's a .click function that's missing ?
<button onclick="getSummary('1');">button</button>
function getSummary(id)
{
$.ajax({ type: "GET",
url: 'ajax_test.php',
data: "user=<?php echo $_GET['user'];?>",
success: function(data) {
$('#summary').html(data);}
});
}
Hi I have function in javascript where I delete photos from database and from server. It work fine, I delete photo but this photo is still in my browser. I have question. How can I refresh only on javascript side?
container.addEventListener("click", function(e){
if(e.target.tagName == 'BUTTON'){
var id = e.target.dataset.type;
var r = confirm("Are You sure to delete?");
if (r == true) {
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html'
});
}
}
});
Now I treid to add
location.reload();
after ajax but I don't want to refresh all page. How Can i delete dynamically from browser?
OK in this type of situation you have two options. Either you load contents using AJAX request or remove the element from DOM either using class or id of that element.
So what happens is if you have loaded content using AJAX you can load all those contents again after successful deletion of image.
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html',
success: function(response) {
$(e.target).remove();
}
});
OR,
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html',
success: function(response) {
// use next ajax query to load content on DOM
}
});
you don't need to reload the whole page to remove the image. Use remove() API from jQuery in the success callback in AJAX call.
$.ajax({
type: 'POST',
url: 'index.php?r=gallery/deletep&name='+id,
dataType: 'html',
success: function(response){
$('img#id').remove();
}
});
I'm calling jAlert with this function
$('.stcommentdelete').live("click",function() {
var ID = $(this).attr("id");
var dataString = 'com_id='+ ID;
jConfirm('Oled sa kindel, et tahad kustutada??', '', function(r){
if(r==true){
$.ajax({
type: "POST",
url: "delete_comment_ajax.php",
data: dataString,
cache: false,
success: function(html){
$("#stcommentbody"+ID).slideUp();
}
});
}
});
});
If i have for example 15 comments and i would delete last comment, it scrolls up. But i need that it stays focused on this place where i delete comment.
Omerimuni
I found what was wrong in my script. I called this script from link like a href="#". I remove href="#" and it works. :D Sry for bother u guys ;)
i have this html:
cancel
when the user clicks cancel, i want to remove the hyperlink and i want to replace this with an image i.e.
$.ajax({
context:this,
type: "POST",
url: "actions/cancel.php",
data: "id=" + the_id,
cache: false,
success: function() {
$(this).remove;
// add image
thanks :))
Try:
success: function() {
$( "a#cancel_3" ).replaceWith( "<img...>" );
}
You'll have to fill in the HTML in the replaceWith function with whatever you want to replace the link with. is just a placeholder I used.
How about:
$(this).css('display', 'none').after('<img>');
Following are two pieces of jquery code. First one prints a text message when clicked on a link, and the second slides down a div when click on a link. I want to merge second code into first one, so that when I click the link, it displays the message (as the first code does), and also slides down the #votebox (as done in second code, and show content in that.
I will be very thankful for any help.
$("a.vote_up").click(function(){
the_id = $(this).attr('id');
$("span#votes_count"+the_id).fadeOut("fast");
$.ajax({
type: "POST",
data: "action=vote_up&id="+$(this).attr("id"),
url: "votes.php",
success: function(msg)
{
$("span#votes_up"+the_id).fadeOut();
$("span#votes_up"+the_id).html(msg);
$("span#votes_up"+the_id).fadeIn();
//Here, I want to slide down the votebox and content div (from code below).
}
});
});
The following code slides down votebox div and displays content in it, I want to include that in the above code.
$("a.vote_up").click(function(){
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
//I want to include this votebox in above code.
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax({
type: "POST",
url: "rating.php",
data: dataString,
cache: false,
success: function(html){
$("#flash").fadeOut("slow");
//and want to use this div as well.
$("#content").html(html);
}
});
});
Thanks for any help.
Simple way is to define a seperate function for voteDown and call it from the success function:
e.g
$("a.vote_up").click(function(){
the_id = $(this).attr('id');
$("span#votes_count"+the_id).fadeOut("fast");
$.ajax({
type: "POST",
data: "action=vote_up&id="+$(this).attr("id"),
url: "votes.php",
success: function(msg)
{
$("span#votes_up"+the_id).fadeOut();
$("span#votes_up"+the_id).html(msg);
$("span#votes_up"+the_id).fadeIn();
var that = this;
voteDown.call(that);
}
});
});
function voteDown()
{
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax({
type: "POST",
url: "rating.php",
data: dataString,
cache: false,
success: function(html){
$("#flash").fadeOut("slow");
//and want to use this div as well.
$("#content").html(html);
}
});
}
Edit: Corrected the JS for Votedown function.