I'm having problems making this code work:
$(function(){
$('div.tags').delegate('input:checkbox', 'change', function() {
var $lis = $('.results > li').hide();
//For each one checked
$('input:checked').each(function() {
$lis.filter('.' + $(this).attr('rel')).show();
});
});
});
With HTML like:
<div class="tags">
<label><input type="checkbox" rel="arts" /> Arts </label>
<label><input type="checkbox" rel="computers" /> Computers </label>
<label><input type="checkbox" rel="health" /> Health </label>
<label><input type="checkbox" rel="video-games" /> Video Games </label>
</div>
<ul class="results">
<li class="arts computers">Result 1</li>
<li class="video-games">Result 2</li>
<li class="computers health video-games">Result 3</li>
<li class="arts video-games">Result 4</li>
</ul>
I've tried it in IE, FF & Opera, but I don't get expected results. That is, the content is not being filtered upon clicking a checkbox? I'm trying to replicate something similar to this:
http://www.houseoffraser.co.uk/Jeans+for+Women/302,default,sc.html
Notice the accordian effect on the left side bar of the page. I'm not too worried about the accordian itself, it's the checkboxes function that I'm focusing on at the moment. On page load, all query results (some 1300 or so), are displayed to the user.
A user can then filter or refine the results by clicking on checkboxes. I'm assuming this is some kind of Jquery/Ajax script, but am not entirely sure? Am I on the right track?
Thanks in advance.
You mentioned in a comment that you're using jQuery 1.3
jQuery's delegate() method was introduced in 1.4.2, so won't be available. You'll have to use a later version of jQuery (any reason why you're using such an old version?).
If you open your developer console (F12 shortcut in Chrome), you should see an error saying that TypeError: Object [object Object] has no method 'delegate'
If you need to use jQuery 1.3, try:
$(function() {
$('div.tags').bind('change', function(e) {
var that = e.originalEvent.target;
if ($(that).is('input:checkbox')) {
var $lis = $('.results > li ').hide();
//For each one checked
$('input:checked ').each(function() {
$lis.filter('.' + $(that).attr('rel')).show();
});
}
});
});
Which you can see working here
$(document).ready(function () {
$('.results > li').hide();
$('div.tags').find('input:checkbox').live('click', function () {
$('.results > li').hide();
$('div.tags').find('input:checked').each(function () {
$('.results > li.' + $(this).attr('rel')).show();
});
});
});
for live demo see this link: http://jsfiddle.net/nanoquantumtech/Ddnuh/
Try this once
$(function(){
$('div.tags').delegate('input:checkbox', 'change', function() {
var $lis = $('.results > li').hide();
//For each one checked
$('input[type="checked"]:checked').each(function() {
$lis.filter('.' + $(this).attr('rel')).show();
});
});
});
Related
My HTMl file has code:
<li id="b1" onclick="myfunc(this)" class="thumbsup fa fa-thumbs-up" ></li>
Above code makes a like button whose color changes on click.
Now I want to get its color on form submission so that on php side I can add number of likes in DB accordingly. That is if user clicked this button then it's color changes to grey . So in jquery i want to see if it is grey and then in php increment value in DB accordingly. Not sure how i can retrieve color of li element in jquery.
Please note that user can click button multiple times. like first time on clicking , button turns grey and when clicked again. it turns to its default color and so on...
As #mplungjan states on the comment you don't need a color to get if it is clicked. You can write an onlick function to send an ajax request to your php file, do the query for the like there and on return you can write a very simple code to color and disable click of the button again.
Like this;
myfunc(e){
$.ajax({
type:'POST',
url:'yourphpfile.php',
data:{whatever:youwanttosend},
success:function(data){
$('#b1').css('background-color':'green');
});
}
You do not want the color of the li, you want its state. Toggle the class:
$(function() {
$(".thumbsup").on("click", function() {
$(this).toggleClass('liked'); // every time we click we set or remove
$(this).next().removeClass('disliked'); // remove from the thumbsdown
})
$(".thumbsdown").on("click", function() {
$(this).toggleClass('disliked'); // every time we click we set or remve
$(this).prev().removeClass('liked'); // remove from thumbsup
})
$("#save").on("click", function() {
var liked = $(".liked").length,
disliked = $(".disliked").length;
console.log(liked,disliked);
// ajax here - sending liked:0,disliked:0 if nothing clicked
$.post("savelike.php",{ "liked":liked,"disliked":disliked},function() {
console.log("saved");
});
})
})
.liked { background-color:green }
.disliked { background-color:red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li id="a1" class="thumbsup fa fa-thumbs-up">👍</li>
<li id="a2" class="thumbsdown fa fa-thumbs-down">👎</li>
</ul>
<button id="save" type="button">Save</button>
Original more complex example with more than one like/dislike set
$(function() {
$(".thumbsup").on("click", function() {
$(this).toggleClass('liked'); // every time we click we set or remove
$(this).next().removeClass('disliked'); // remove from the thumbsdown
})
$(".thumbsdown").on("click", function() {
$(this).toggleClass('disliked'); // every time we click we set or remve
$(this).prev().removeClass('liked'); // remove from thumbsup
})
$("#count").on("click", function() {
console.log($(".liked").length,"liked",
$(".disliked").length,"disliked"); // how many
// which ones - using http://api.jquery.com/map/
let likes = $('.thumbsup.liked').map(function(){ // array map
return this.id; // each ID that is liked
}).get();
let dislikes = $('.thumbsdown.disliked').map(function(){
return this.id
}).get();
// here are the arrays - use .join(",") to get a list
console.log(likes.length>0?likes:"No likes",
dislikes.length>0?dislikes:"No dislikes")
})
})
.liked { background-color:green }
.disliked { background-color:red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li id="a1" class="thumbsup fa fa-thumbs-up">👍</li>
<li id="a2" class="thumbsdown fa fa-thumbs-down">👎</li>
<li id="b1" class="thumbsup fa fa-thumbs-up">👍</li>
<li id="b2" class="thumbsdown fa fa-thumbs-down">👎</li>
</ul>
<button id="count" type="button">Count</button>
You can use find function of jQuery.
Suppose you have multiple li under div element than you can try the following code to get all li elements which color is grey.
$( "div.classname" ).find( "li.thumbsup" ).css( "background-color", "grey " ).
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.
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.
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.
i have a problem with this jquery, in the success function call, its mean to remove the `support' button, and fade in the msg, this is my jquery code:
$('.stats').delegate('.against', 'click', function(e) {
//stop event
e.preventDefault();
// cache a reference to the previous <li> element
// since it is used more than once
var $prevLi = $(this).closest('li').prev('li');
//get the id
var the_id = $prevLi.attr('id').split('_').pop();
//the main ajax request
$.ajax({
context:this,
type: "POST",
data: "action=against&id=" + the_id,
url: "ajax/sa.php",
success: function (msg) {
$prevLi.find("h2.score_down").html(msg).fadeIn();
$(this).closest('li').next().next().find('button').remove(); $(this).remove();
}
});
});
the html:
<ul class="stats">
<li id="topic_20" class="score">
<h2 class="score_up" style="color:green;">10</h2>
<span style="text-align:center;">Supporters</span>
</li>
<li>
<button type="submit" value="Actions" class="support" title="support">
<i></i>
<span>Support</span>
</button>
</li>
<li id="down_20"class="score"><h2 class="score_down">20</h2><span style="text-align:center;">Against</span>
</li>
<li>
<button type="submit" value="Actions" class="against" title="against">
<i></i><span>Against</span></button>
</li>
</ul>
<h3 class="success"></h3>
this jquery is meant to remove the support button, when clicked and the new score is meant to fade in! :)) thanks
The button is in the <li> 2 previous to the .against containing one, so your .next() calls should be .prev(), like this:
$(this).closest('li').prev().prev().find('button').remove();
Why not use $("button[type=submit]")? Or just $("button")? If you use the double prev() you're going to have to adjust your code when your markup changes. If you do that often enough, that makes making changes a nightmare later.