jquery toggle issue in firefox - php

The below code works fine in Chrome, but doesn't work correctly in Firefox.
In Chrome:
Expanding the hide/show link shows the up arrow correctly when expanded and the down arrow when collapsed
In Firefox:
Expanding the hide/show link shows the up arrow correctly when expanded, but when collapsed it still shows the up arrow.
What am I doing wrong?
Thanks
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var $divView = $('div.view');
var innerHeight = $divView.removeClass('view').height();
$divView.addClass('view');
$('div.slide').click(function() {
// Update the HTML in this element
var slideHtml = $(this).html();
// Switch between show/hide
if (slideHtml.localeCompare('Hide / Show <img src="images/arrow_up.png" />') < 0)
$(this).html('Hide / Show <img src="images/arrow_up.png" />');
else
$(this).html('Hide / Show <img src="images/arrow_down.png" />');
$('div.view').animate({
height: (($divView.height() == 90)? innerHeight : "90px")
}, 500);
return false;
});
});
</script>
<div class="view">
<ul>
<li>text here</li>
<li>text here</li>
<li>text here</li>
<li>text here</li>
<li>text here</li>
<li>text here</li>
</ul>
</div>
<div class="slide">Hide / Show <img src="images/arrow_down.png" /></div>

You should use toggle() it is much simpler and it avoids you all this.
I made you an example here: http://jsfiddle.net/MVnbM/
HTML:
Click me
<div class="arrowUp">Up</div>
<div class="arrowDown">Down</div>
JS:
$('.arrowUp').hide();
$('a.clickMe').click(function() {
$('.arrowUp, .arrowDown').toggle();
});
Here's your code modified:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var $divView = $('div.view');
var innerHeight = $divView.removeClass('view').height();
$divView.addClass('view');
$('.arrowUp').hide();
$('div.slide').click(function() {
$('.arrowUp, .arrowDown').toggle();
});
});
<div class="view">
<ul>
<li>text here</li>
<li>text here</li>
<li>text here</li>
<li>text here</li>
<li>text here</li>
<li>text here</li>
</ul>
</div>
<div class="slide">Hide / Show <img src="images/arrow_down.png" class="arrowUp" /><img src="images/arrow_down.png" class="arrowDown" /></div>

What about this variant using toggle and toggleClass
http://jsfiddle.net/347pg/

Related

Active class in navigation menu in PHP

I want to manage the display of my active class on the navigation bar.
I have a first home page with a button and when i click on this button. I want to arrive on the second page with a navigation menu with
<ul> <li> tags
And I want that when clicking on the button on the home page, by default I select the first navigation menu with a background color, and clicking on the other menus applies the same style to the clicked menu and deletes the active class on the old menu.
<pre>
//home page
<div>
Voir Menu
</div>
// Menu page
<nav class="nav-bloc-menu">
<ul id="list1">
<li id="testmenu" <?php if ($pec == 22) {echo "class=\"p22\"";}?>><a href="m1.php">Menu
1</a></li>
<li <?php if ($pec == 23) {echo "class=\"p23\"";}?>>Menu 2</li>
<li <?php if ($pec == 24) {echo "class=\"p24\"";}?>>Menu 3</li>
</ul>
</nav>
//jquery
$(".nav-bloc-menu ul a").click(function(){
$(".nav-bloc-menu ul a").css("background-color","rgba(255,255,255,1)");
$(this).css("background-color","#1c2335");
$(this).css("color","white");
});
</pre>
my index.php page
<pre>
<?php
$pagetitle = "Page";
$current_page = "menu.php";
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<link rel="stylesheet" href="style.css">
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script src="script.js">
</script>
</head>
<body>
<div class="card">
<a href="menu.php" class="card-link">Voir Menu
</a>
</div>
</body>
</html>
</pre>
my menu.php page
<pre>
<nav class="nav-bloc-menu">
<ul id="list1">
<li class="<?php if ($current_page == "p1.php" || $current_page ==
"menu.php")
{?>active<?php }?>">p1<li>
<li class="<?php if ($current_page == "p2.php") {?>active<?php }?>">p2<li>
<li class="<?php if ($current_page == "p3.php") {?>active<?php }?>">p3<li>
</ul>
</nav>
</pre>
my script.js page
<pre>
$(document).ready(function(){
$(".nav-bloc-menu ul a").click(function(){
$(".nav-bloc-menu ul a").css("background-color","rgba(255,255,255,1)");
$(this).css("background-color","#1c2335");
$(this).css("color","white"); });
});
</pre>
my style.css page
<pre>
.nav-bloc-menu li .active{ background-color:#1c2335;color:white; }
</pre>
Above is the code i am using but doesn't work, can anyone help?
Otherwise just do based on page ie.(index.php,about.php) and just write class .nav-menu li .active{ background:white;color:black; }
<?php
$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri_segments = explode('/', $uri_path);
$current_page= $uri_segments[0]; //ex: https://xxxx.com/about.php
?>
<ul class="nav-menu">
<li class="<?php if($current_page=="index.php"){ ?>active<?php } ?>">Home</li>
<li class="<?php if($current_page=="about.php"){ ?>active<?php } ?>">Home</li>
</ul>
I think this will help you.
Just add document.ready then only it work. just check this one.
$(document).ready(function(){
$(".nav-bloc-menu ul a").click(function(){
$(".nav-bloc-menu ul a").css("background-color","rgba(255,255,255,1)");
$(this).css("background-color","#1c2335");
$(this).css("color","white"); });
});
// if suppose using ajax page mean you need do like this
$(document).on("click",".nav-bloc-menu ul a",function(){
$(".nav-bloc-menu ul a").css("background-color","rgba(255,255,255,1)");
$(this).css("background-color","#1c2335");
$(this).css("color","white");
})
Sure this will work just try.

Hide/show element after append

$("ol").append("<div id='save' style='display:none'>Save<div>");
$("li").mouseup(function() {
$('#save').show();
});
$("ol").sortable();
$("body").on('click','#save',function(){
$(this).hide();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>
<ol id='some123'>Items
<li draggable='true'>Item 1</li>
<li draggable='true'>Item 2</li>
<li draggable='true'>Item 3</li>
<li draggable='true'>Item 4</li>
</ol>
snippet above working correctly with js append.
How to show an element after append by php using mouseup?
So, I have draggable list. And, I need to show element every time the user changes the list order. Every time the element is dropped, I call a function as shown:
$("li").mouseup(function() {
$('#save').show();
});
$("ol").sortable();
When the user clicks save button, I hide it using:
$("body").on('click','#save',function(){
$(this).hide();
})
But how do I make it visible again? $('#save').show(); isn't working!
funniest thing:
my php generate 2 buttons, like that:
<div class="buttons"><button id="saveTit" style="display: none" >Save content</button></div>
<div class="buttons"><button class="butClick" data-con='<?php echo $content?>'>Add new row</button></div>
To call 2nd button i use:
$(".butClick").on('click',function(){
alert($(this).attr('data-tit'));
})
but to call 1st one i have to use body selector... any idea why?!
I change id for class inside div and to call a event...
And it works...
Hi try to change the div to hidden instead and use the prop function instead of show and hide :
$("ol").append("<div id='save' hidden>Save<div>");
$("li").mouseup(function() {
$('#save').prop("hidden",false);
});
$("ol").sortable();
$("body").on('click','#save',function(){
$(this).prop("hidden",true);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>
<ol id='some123'>Items
<li draggable='true'>Item 1</li>
<li draggable='true'>Item 2</li>
<li draggable='true'>Item 3</li>
<li draggable='true'>Item 4</li>
</ol>
Hope it helps

Change background image on menu hover in Wordpress

Here is my site (click here) I 'm trying to get the background image to change on hover of a menu item, and I'm getting close! However, when I hover, the menu disappears and the swap back to the original image is not smooth like I'd like. Any ideas on how I can improve this??
<?php
/*
Template Name: Page - All Projects
*/
?>
<?php get_header(); ?>
<?php
/* #Start the Loop
======================================================*/
if (have_posts()) : while (have_posts()) : the_post();
?>
<?php
/* #Get Fullscreen Background
======================================================*/
$pageimage = get_post_meta($post->ID,'_thumbnail_id',false);
$pageimage = wp_get_attachment_image_src($pageimage[0], 'full', false);
ag_fullscreen_bg($pageimage[0]);
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
//rollover swap images with rel
var img_src = "$pageimage";
var new_src = "";
$(".rollover").hover(function(){
//mouseover
img_src = $(this).attr('src'); //grab original image
new_src = $(this).attr('rel'); //grab rollover image
$(this).attr('src', new_src); //swap images
$(this).attr('rel', img_src); //swap images
},
function(){
//mouse out
$(this).attr('src', img_src); //swap images
$(this).attr('rel', new_src); //swap images
});
//preload images
var cache = new Array();
//cycle through all rollover elements and add rollover img src to cache array
$(".rollover").each(function(){
var cacheImage = document.createElement('img');
cacheImage.src = $(this).attr('rel');
cache.push(cacheImage);
});
})(jQuery);
</script>
<div class="contentarea">
<div id='cssmenu'>
<ul>
<li class='has-sub '><a href='#'>Center Consoles</a>
<ul>
<li class='sub'><a href='http://takeitto11.com/striper2015/portfolio/2oo-cc/'><img class="rollover" width="1500px" height="1000px" rel="http://takeitto11.com/striper2015/wp-content/uploads/2014/10/Striper_HPS_1500x150010.jpg" />200 CC</a></li>
<li class='sub'><a class="220CC" href='#'>220 CC</a></li>
<li class='sub'><a class="2605CC" href='#'>2605 CC</a></li>
</ul>
</li>
<li class='has-sub '><a href='#'>Dual Consoles</a>
<ul>
<li class='sub'><a class="200DC" href='#'>200 DC</a></li>
<li class='sub'><a class="220DC" href='#'>220 DC</a></li>
</ul>
</li>
<li class='has-sub '><a href='#'>Walk Arounds</a>
<ul>
<li class='sub'><a class="200WA" href='#'>200 Walk Around</a></li>
<li class='sub'><a class="220WA" href='#'>220 Walk Around</a></li>
<li class='sub'><a class="2601WA" href='#'>2601 Walk Around</a></li>
<li class='sub'><a class="2901WA" href='#'>2901 Walk Around</a></li>
</ul>
</li>
</div>
<div class="clear"></div>
</ul>
Thank You!!

Jquery not adding or Removing my selected class for my nav menu

I wrote my site with php. I have the index file dynamically loading my pages from a pages folder. I have the nav menu in a separate file as well.
so I'm trying to have the text color highlighted when clicked, since I can't get the windowlocation to work. Because of the way I have it setup, the windowlocation returns /index.php with javascript or jquery. So I tried to keep it simple by just targeting the link.
<nav>
<ul>
<li>Home</li>
<li>Skills</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!--Script to add selected class to highlight active link-->
<script>
$(document).ready(function(){
$("ul li a").click(function(){
$("ul li a").removeClass('selected');
$("ul li a").addClass('selected');
});
});
</script>
my css is setup like this:
a.selected{color:#0066ff;}
I've tried throwing this code in every php file I created. Still nothing works. When I want an alert box to pop up with a message when a link is clicked, that works fine. why does this not work? Nothing changes when I run the code... Any suggestions?
If you reload the page by clicking each link then try
<nav>
<ul>
<li><a href="index.php?p=home" <?php (empty($_GET['p']) || $_GET['p'] == 'home') ? echo 'class="selected"' : ''?> >Home</a></li>
<li><a href="index.php?p=skills" <?php $_GET['p'] == 'skills' ? echo 'class="selected"' : ''?>>Skills</a></li>
<li><a href="index.php?p=about" <?php $_GET['p'] == 'about' ? echo 'class="selected"' : ''?>>About</a></li>
<li><a href="index.php?p=contact" <?php $_GET['p'] == 'contact' ? echo 'class="selected"' : ''?> >Contact</a></li>
</ul>
</nav>
Though you won't need the jQuery function.
If you don't want to reload the page then try this in your jQuery:
$(document).ready(function(){
$("ul li a").click(function(){
$("ul li a").removeClass('selected');
$(this).addClass('selected');
return false;
});
});
$('#menu li a').on('click', function(){
$('li a.current').removeClass('current');
$(this).addClass('current');
});
You need to add selected class in the clicked li element.
try like this:
$(document).ready(function(){
$("ul li a").click(function(){
$(".selected").removeClass('selected');
$(this).addClass('selected');
});
});
Actually you are redirecting when you are clicking in the li element. so jquery wil not be worked in this scenario.
it will work if you use one page application with out page reloading.
You can to do it by php like this:
// index.php
<?php
$page = isset($_REQUEST['p']) ? $_REQUEST['p'] : '';
switch($page) {
case 'skills' :
echo ' <ul>
<li>Home</li>
<li>Skills</li>
<li>About</li>
<li>Contact</li>
</ul>';
break;
case 'about' :
echo ' <ul>
<li><a href="index.php?p=home" >Home</a></li>
<li>Skills</li>
<li>About</li>
<li>Contact</li>
</ul>';
break;
case 'contact' :
echo ' <ul>
<li><a href="index.php?p=home" >Home</a></li>
<li>Skills</li>
<li>About</li>
<li>Contact</li>
</ul>';
break;
default:
echo ' <ul>
<li>Home</li>
<li>Skills</li>
<li>About</li>
<li>Contact</li>
</ul>';
}

Display Hex Code on Page load

The correct, working code can be seen on the replies. End result: http://www.creativewebgroup.co.uk/library/colorshareV2/palette/Android
I'm attempting to make a colour palette script.
I have this jQuery script:
<script>
//document ready
$(document).ready(function () {
$('.palette-detail li').each(function () {
$(this).html('<input type="text" style="background: #' + $(this).attr('swatch') + '" />' );
});
$('.palette-detail').click(function (e) {
var elem = e.target;
if ($(elem).is('input')) {
$(elem).val($(elem).parent().attr('swatch'));
}
});
});
Here's a basic idea of the HTML used (in the script however it's PHP driven).
<ul class="palette">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<span>Title</span>
</ul>
At the moment the script requires the user to click on a li block for the hex code to display. I want it to instead show straight away.
Is this possible? If so how?
Thanks a bunch guys!
HTML
<ul class="palette">
<li swatch="#4362ff">
<li swatch="#ee3d5f">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
<li swatch="#FFFFFF">
</ul><span>Title</span>
jQuery
//document ready
$(document).ready(function () {
$('.palette li').each(function () {
$(this).html('<input value='+$(this).attr("swatch")+' type="text" style="background: ' + $(this).attr('swatch') + '" />');
});
});
fiddle
There were lots of mistakes in the code. You didn't select the correct class for the UL.
Also UL elements can not contain span elements.
Also using inspect element would have showed the code does what it told it to and put ## on front of the color.

Categories