JQuery Compare Class of List Items - php

Using JQuery I want to check that each list item has the css class Complete. If so, i want to display a button, if not I want to keep the button hidden.
The classes are currently inserted dynamically using PHP when the page loads.
My code so far is;
<button id="steps-complete" hidden>Download Now</button>
<ul class="wb-steps">
<li class="<?php echo $class ?>">Step 1</li>
<li class="<?php echo $class ?>">Step 2</li>
<li class="<?php echo $class ?>">Step 3</li>
</ul>
<script>
jQuery( document ).ready(function() {
var steps = [];
jQuery( ".wb-steps li" ).each(function( index ) {
// successfully displays complete for each list item
console.log( index + ": " + jQuery( this ).attr('class') );
// if all stepa have 'complete' show button
$("#steps-complete").show();
});
});
</script>

You have the $class variable, and its value is set as the class of all list items.
Since you are using PHP to render the page, you can use the same variable to test if the button should be shown or not. So you wouldn't need jQuery in this case, you can just remove that part.
Here's what it would look like:
<?php
if (strpos($class, 'Completed') !== false) {
?>
<button id="steps-complete" hidden>Download Now</button>
<?php
}
?>
This works in your case because you set the same class to all items.
Hope this helps.

i've made a function to check of the children of the wb-steps have a class named john, if not then we show the steps complete
jQuery( document ).ready(function() {
if($(".wb-steps").children().not('.john').length = 0){
console.log("there was a div found with a class named john");
}else{
$("#steps-complete").show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="steps-complete" hidden>Download Now</button>
<ul class="wb-steps">
<li class="<?php echo $class ?>">Step 1</li>
<li class="<?php echo $class ?>">Step 2</li>
<li class="<?php echo $class ?>">Step 3</li>
</ul>

You can simply count the elements to see if all of them have the complete class:
jQuery(document).ready(function() {
var $steps = $(".wb-steps li");
var show = $steps.length === $steps.filter('.complete').length;
$("#steps-complete")
.toggle(show)
.get(0)
.toggleAttribute('hidden', show);
});

Related

Data attribute only returning first value

I'm trying to get the body of my page to change color when hovering over some list items. Each list item has its own color stored in a data attribute, which I can see in the chrome inspector. The code is doing what I'd like, but returning only the first color in the list for every item, when I want the body to be the color of each list item.
HTML:
<ul class="menu">
<?php foreach($page->children() as $subpage): ?>
<li id="tesq" data-color="<?= $subpage->color() ?>">
<a href="<?= $subpage->url() ?>">
<?= html($subpage->title()) ?></a>
</li>
<?php endforeach ?>
</ul>
jQuery:
$(function() {
$('li').hover(function() {
$("body").css('backgroundColor', function () {
return $("#tesq").data('color')
});
}, function() {
$("body").css('backgroundColor', 'lightgrey')
});
})
Any help much appreciated
In your current code, you are trying to get data attribute from the entire collection so it will return the data attribute value of the first element among the collection.
In addition to that use class for a group of elements instead of the id(id should be unique in the context - $("#tesq") will select only the first element).
So do it based on the hovered element, where you can use this inside the event handler callback to refer the eleemnt.
PHP :
<ul class="menu">
<?php foreach($page->children() as $subpage): ?>
<li class="tesq" data-color="<?= $subpage->color() ?>">
<a href="<?= $subpage->url() ?>">
<?= html($subpage->title()) ?></a>
</li>
<?php endforeach ?>
</ul>
JQUERY :
$(function() {
$('.tesq').hover(function() {
var $this = $(this);
$("body").css('backgroundColor', function () {
return $this.data('color')
});
}, function() {
$("body").css('backgroundColor', 'lightgrey')
});
})
The callback is completely unnecessary here and you can avoid it.
$(function() {
$('.tesq').hover(function() {
$("body").css('backgroundColor', $(this).data('color'));
}, function() {
$("body").css('backgroundColor', 'lightgrey')
});
})

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>';
}

jQuery and Wordpress links not working

Got a problem with my WordPress menu. After I insert the jquery my links (in the menu and in sidebars won't work. What to do? Thank you!
My code:
HTML
<ul class="menu">
<li class="menu-item">
Link text
<ul class="sub-menu">
<li><a>Text</a></li>
<li><a>Text</a></li>
<li><a>Text</a></li>
</ul>
</li>
</ul>
jQuery
$(document).ready(function(){
$('li.menu-item').each(function() {
var $dropdown = $(this);
$($dropdown).click(".menu-item a", function(e) {
e.preventDefault();
$ul = $("ul.sub-menu", $dropdown);
$('ul.sub-menu').toggle();
$("ul.sub-menu").not($ul).hide();
return false;
});
});
$('html').click(function(){
$("ul.sub-menu").hide();
});
});
There might be a problem with the event propagation. If a link within a sub-menu is clicked the click event propagates to the surrounding menu-item and triggers your JS code.
Please try to add this code to prevent the propagation effect:
$( "ul.sub-menu a" ).click(function( event ) {
event.stopPropagation();
});
please fix the issue by replacing below script
<li class="menu-item> to <li class="menu-item">

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.

save sort oder and populate sorted records jquery UI Sortable

hi i am using jquery ui sortable to sort my divs . i can get the div oder to save in the db , this is my code
<div style="margin:0 auto !important;">
<ul id="sortable1" class="connectedSortable">
<li id="one_1" class="ui-state-default"><div>Item 1</div></li>
<li id="one_2" class="ui-state-default"><div>Item 2</div></li>
<li id="one_3" class="ui-state-default"><div>Item 3</div></li>
<li id="one_4" class="ui-state-default"><div>Item 4</div></li>
<li id="one_5" class="ui-state-default"><div>Item 5</div></li>
</ul>
<ul id="sortable2" class="connectedSortable">
<li id="two_1" class="ui-state-highlight"><div>Item 1</div></li>
<li id="two_2" class="ui-state-highlight"><div>Item 2</div></li>
<li id="two_3" class="ui-state-highlight"><div>Item 3</div></li>
<li id="two_4" class="ui-state-highlight"><div>Item 4</div></li>
<li id="two_5" class="ui-state-highlight"><div>Item 5</div></li>
</ul>
</div>
<script type="text/javascript">
jQuery(function() {
var sortable1 = 'one_1,one_2,one_3,one_4,one_5';
var sortable2 = 'two_1,two_2,two_3,two_4,two_5';
jQuery( "#sortable1, #sortable2" ).sortable({
update: function(event, ui) {
var newOrder = jQuery(this).sortable('toArray').toString();
if(jQuery(this).attr('id')=="sortable1"){
sortable1 = newOrder;
}
else {
sortable2 = newOrder;
}
console.log(sortable1);
console.log(sortable2);
}
}).disableSelection();
});
</script>
i can save the sorted div order to DB correctly , but i have no idea how to populate the div's to correct order again . please help me . thanks in advance .
Instead of saving the sort order to the DB as the comma separated value 1,2,3,4,5, why not just save the 'sort column' e.g. #sortable1 such that when you are loading the divs you just pass this value to your js logic.
Example:
if you save the sort column name say #sortable1 or #sortableN in the DB then you end up with
var newOrder = jQuery(this).sortable('toArray').toString();
if(jQuery(this).attr('id')== "<% $your_php_variable %>"){
sortable1 = newOrder;
}
else {
sortable2 = newOrder;
}
Am not sure how you are passing the php variable to the UI that's what i've put as <% $your_php_variable %>. At some point in your php code you will have set $your_php_variable = '#sortableX';

Categories