jQuery .load issue when switching content - php

Tristann.tk
On this site click About me and then click back to Home, and then try switching the pages at the bottom, It doesn't change. Why?
jQuery code:
$(document).ready(function(){
$(".page_links .page li").click(function(){
$(".page_links .page li").css({'background-color' : ''});
$(this).css({'background-color' : '#A5CDFA'});
$(".posts").load("/includes/data.php?data=pages&page=" + this.className);
});
$(".navlist #about_me").click(function(){
$(".navlist #home").css({'text-decoration' : 'none'});
$(".navlist #about_me").css({'text-decoration' : 'underline'});
$(".vsebina").load("/includes/data.php?data=about_me");
});
$(".navlist #home").click(function(){
$(".navlist #about_me").css({'text-decoration' : 'none'});
$(".navlist #home").css({'text-decoration' : 'underline'});
$(".vsebina").load("/includes/data.php?data=home");
});
$(".navlist #home").css({'text-decoration' : 'underline'});
$(".1").css({'background-color' : '#A5CDFA'});
});
The divs are like that:
<div class="zunanji">
<div class="glava">
<div class="meni">
<ul class="navlist">
<li><a id="home" href="javascript:void(0)">Home</a></li>
<li><a id="about_me" href="javascript:void(0)">About Me</a></li>
</ul>
</div>
</div>
<div class="container">
<div class="vsebina">
<? get_posts();
get_pages();
?>
</div>
<div class="stranska">
<h2>Archive</h2>
</div>
</div>
</div>
The 2 PHP functions return the 5 posts at the start and the page numbers at the bottom.

You'll need to rebind those events after every load because the page_links are being replaced. An easier and cleaner way to do it is to use event delegation. The best way is to use $.fn.on, which performs better and is less buggy than $.fn.live. Also, since the load will replace the page_links you have to do the background-color stuff in a callback:
$(".vsebina").on('click', '.page_links .page li', function(){
var $page = $(this); // save the page link that was clicked
var path = "/includes/data.php?data=pages&page=" + this.className;
$(".posts").load(path, function(){
$(".page_links .page li").css({'background-color' : ''});
$page.css({'background-color' : '#A5CDFA'});
});
});
This binds a click event to the .vsebina container and checks to see if the clicked item was a page link. If it was, it will fire your handler. This way, even if the content is replaced, your handler will run.
Read more about $.fn.on vs $.fn.live vs $.fn.delegate vs $.fn.bind here

Your click events are getting disconnected when you load new content. Change your page nav code to use the live() method:
$(".page_links .page li").live('click',function(){
$(".page_links .page li").css({'background-color' : ''});
$(this).css({'background-color' : '#A5CDFA'});
$(".posts").load("/includes/data.php?data=pages&page=" + this.className);
});

Related

Reload content in div with switch case include

I am kinda new in Ajax and jQuery the problem is that I have a folder pages and in it are php pages with content divs. I want to reload the container div with the content instead of the whole page i tried many things but nothing seems to help. this is a part of how my index.php looks like:
<?php include ('inc/nav.php'); ?>
<div id="container">
<?
$type = $_GET["type"];
switch ($type) {
case "about" :
include "pages/about-us.php";
break;
case "contact":
include "pages/contact.php";
break;
default :
include "pages/homepage.php";
}
?>
</div>
In the navigation the link looks like this:
<ul class="main-menu">
<li class="url">Homepage</li>
<li class="url">About us</li>
<li class="url">Contact</li>
</ul>
I tried to get the content that's in pages/about-us.php into the <div id="container"></div> but it's not working. this is how my ajax script looks like:
//run on page load
$(function(){
//bind a click event to the nav links
$(".url a").on("click", function(e){
//keep the links from going to another page by preventing their default behavior
e.preventDefault();
//this = link; grab the url
var pageLocation = this.href;
console.log(pageLocation);
//fire off an ajax request
$.ajax({
url: pageLocation,
//on success, set the html to the responsetext
success: function(data){
$("#container").html(data.responseText);
}
});
});
});
The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.
Consider the following example.
$(function() {
$(".url a").click(function(e) {
e.preventDefault();
var pageLocation = this.href + " #container";
console.log(pageLocation);
$("#container").load(pageLocation)
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="main-menu">
<li class="url">Homepage</li>
<li class="url">About us</li>
<li class="url">Contact</li>
</ul>
<div id="container">
</div>
This is untested. You will want to test this in your own code.
See More: https://api.jquery.com/load/

Avoiding appending of previous page parameter when clicking on jquery tabs

In my webpage I am using jquery tabs.
<script type="text/javascript">
$(document).ready(function()
{
$('#horizontalTab').responsiveTabs({
rotate: false,
startCollapsed: 'accordion',
collapsible: 'accordion',
setHash: true,
disabled: [3,4],
activate: function(e, tab) {
$('.info').html('Tab <strong>' + tab.id + '</strong> activated!');
}
});
$('#start-rotation').on('click', function() {
$('#horizontalTab').responsiveTabs('active');
});
$('#stop-rotation').on('click', function() {
$('#horizontalTab').responsiveTabs('stopRotation');
});
$('#start-rotation').on('click', function() {
$('#horizontalTab').responsiveTabs('active');
});
$('.select-tab').on('click', function() {
$('#horizontalTab').responsiveTabs('activate', $(this).val());
});
});
</script>
<div id="horizontalTab" style="margin-top:10px;">
<ul class="tabul">
<li class="tabli">
<?php echo lang('purchasedtickets');?>
</li>
<li class="tabli"><?php echo lang('gifted').' '.lang('tickets');?></li>
<li class="tabli"><?php echo lang('received').' '.lang('tickets');?></li>
</ul>
<div id="purchased"></div>
<div id="gifted"></div>
<div id="received"></div>
</div>
When I click on tab2 ie, #gifted tab, the corresponding result will be fetched from an ajax call and will be set to div with id gifted. In this sections I am using Codeigniter pagination. If I click on pagination link 2 of gifted section, the URL will come like http://domain.org/project/video/tickets/2#gifted where 2 in the URL is the page number.
After this, when I click on any other tab say tab1 ie, purchased tab, then the link of page will come like http://domain.org/project/teshot/video/tickets/2#purchased (instead of http://domain.org/project/teshot/video/tickets#purchased) which is appending the url of previous section.
I want to avoid this problem. How can I solve this?
Can ayone help me?
Thanks in advance.

AJAX load PHP file

I am a newbie on AJAX, I have a link that loads table.php. Then it writes the code to the index.php. In that code, I have another link to
show the info.php. Is it possible to do this?
<!--This is index.php-->
<div id="link">my Info</div><!--it works here-->
<div id="link">My Table</div>
<div id="table"></div>
<div id="info"></div>
<!--My javascript-->
<script type="text/javascript">
$(document).ready(function() {
$('#link a').click(function(){
var page = $(this).attr('href');
if(page =='table')
$('#table').load('table.php');
else if(page =='info')
$('#info').load('info.php');
return false;
})
});
</script>
<!--This is table.php-->
<div id="link">my Info</div><!--it doesn't works here-->
<!--This is info.php-->
<h1>My info</h1>
Your three <div> (as pointed out by #scragar) have the same id link, most probably causing the issue. Make it a class like that :
<div class="link">
And in your JS :
$('.link a')
EDIT : As noted by dbf, you must as well declare your handler with live() or on() instead of click() :
$('.link a').live('click', function(){ ... });
in order for it to be binded after table.php is loaded in the page. ( http://api.jquery.com/live/ )

This JavaScript Function Works Alone, But Not With Other JavaScript Functions

I have this javascript :
<script type="text/javascript">
$(document).ready(function($) {
$('#target-share ul li').click(function() {
$('input#shareto').val($(this).data('val'));
});
});
</script>
and it really works as I expected. but when I add this another javascript to do drop-down animation, that javascript not working anymore :
<script type="text/javascript">
$(document).ready(function() {
$("#select-shareto a").click(function () {
var newClass = $(this).find("span").attr('class');
var newText = $(this).find("span").text();
$("#select-shareto a").removeClass("selected");
$(this).addClass("selected");
$("a#to-chosen span").removeClass().addClass(newClass).text(newText);
$("a#to-chosen").toggleClass("opened");
$('#select-shareto').slideToggle('fast');
return false;
});
$('a#to-chosen').click(function() {
$(this).toggleClass("opened");
$('#select-shareto').slideToggle('fast', function() {
// Animation complete.
});
});
});
</script>
those javascript actually affected on this HTML code :
<div id="target-share">
<span class="to-admin">Admin</span>
<ul id="select-shareto">
<li data-val="0-1">
<span class="to-Finance">Finance</span>
</li>
<li data-val="1-1">
<span class="to-admin-private">Admin Private</span>
</li>
<li data-val="1-0">
<span class="to-ceo">CEO</span>
</li>
<li data-val="0-0">
<span class="to-ceo-private">CEO Private</span>
</li>
</ul>
<input id="shareto" type="text" value="0-1" name="shareto">
</div><!-- #target-share -->
any idea how to make those 2 javascript works side-by-side? thanks.
$("#select-shareto a").click(function () {
...
return false;
});
Hereby, you stop the further propagation of this event - other handlers for this click event won't be called any more. Instead, use this:
$("#select-shareto a").click(function (e) {
e.preventDefault();
...
});
(Demo at jsfiddle.net)
I'm assuming you tried to insert the code where the comment animation complete is and if so you just need to put the code:$('#target-share ul li').click(function() { $('input#shareto').val($(this).data('val'));});
Your code is already wrapped in a $(document).ready so you don't need another one.

jquery tab cookie ...?

Hy ,
I have the following code for mai tabs ...
$(document).ready(function() {
$("#continut_right div.tab").hide();
$("#continut_right div.tab:first-child").show();
$("#navigatie_left ul li").click(function() {
var id_tab = $(this).attr("id");
$("#continut_right div.tab").hide();
$("#continut_right").children("#" + id_tab).show();
});
and the markup is something like this :
<div id="continut_right">
<!-- tab 1 -->
<div class="tab" id="id1">
content ... bla bla bla
</div>
</div>
and the menu that trigger tabs to change :
<div id="navigatie_left">
<a style="text-decoration: none; color:#000; font-family:Arial; font-size:15px; font-weight:bold;" href="<?php echo $_SERVER['PHP_SELF']; ?>">[ REFRESH ]</a>
<br /><br />
<ul>
<li id="id1">Categorii principale</li>
<li id="id2">Categorii secundare</li>
<li id="id2">Texte categorii secundare</li>
</ul>
</div>
How can i implement a code ... to remember what tab was opened because when i do a form submit to php_self to keep that tab open to see changes ... ??
Thanks a lot
You can use window.location.hash to store the current tab id. You have to write code that reads the window.location.hash property for the tab id on $(document).ready() and automatically switch to that tab.
An example:
$(document).ready(function(){
var id_tab = window.location.hash;
$("#continut_right div.tab").hide();
if(id_tab){
$("#continut_right").children(id_tab).show();
}else{
$("#continut_right div.tab:first-child").show();
}
$("#navigatie_left ul li").click(function() {
var id_tab = $(this).attr("id");
$("#continut_right div.tab").hide();
$("#continut_right").children("#" + id_tab).show();
window.location.hash = id_tab;
});
});
You could look into .appendTo()'s JSON Cookie solution. it would offer an easy method of storing and retrieving the last-active tab.
The usage is documented as follows:
$.cookie( string key, mixed value [, hash options ] )
So you could store the index of the active tab:
$.cookie( 'activeTab', '3' );
And then retrieve it on page load with:
$.cookie( 'activeTab' ); //returns the set value
So let's look at how you could implement it:
$("#navigatie_left ul li").click(function() {
var id_tab = $(this).attr("id");
$.cookie( 'activeTab', id_tab ); // storing the new active tab
$("#continut_right div.tab").hide();
$("#continut_right").children("#" + id_tab).show();
});
Now for us to automatically show the tab on page load:
$(document).ready(function() {
$("#continut_right div.tab").hide();
$("#continut_right div.tab:first-child").show();
// Which tab should we show on document.ready?
$("#continut_right").children("#" + $.cookie('activeTab')).show();
});

Categories