How to create a selector tool with jquery - php

I am looking to create a dog breed selector tool where users can narrow down dog breeds by selecting certain characteristics. This is what I have so far:
$(document).ready(function(){
$("#big,#long").click(function(){
$("#pug").hide();
});
$("#small,#long").click(function(){
$("#bull").hide();
});
$("#big,#short").click(function(){
$("#pom").hide();
});
$("#small,#short").click(function(){
$("#new").hide();
});
});
</script>
<p>Size</p>
<button id="small">Small</button>
<button id="big">Big</button>
<p>Coat</p>
<button id="short">Short Hair</button>
<button id="long">Long Hair</button>
<p id="pug">Pug</p>
<p id="bull">Bulldog</p>
<p id="pom">Pomeranian</p>
<p id="new">Newfoundland</p>
I've tried incorporating .show() in order to get the breeds to reappear after another characteristic is reselected, but then all of the breeds reappear which defeats the purpose of the tool.
I know a little php, so let me know if it would be easer using it instead.
Thanks

It would be easier if you just added the specific class to the name (demo):
<p id="pug" class="small short">Pug</p>
<p id="bull" class="big short">Bulldog</p>
<p id="pom" class="small long">Pomeranian</p>
<p id="new" class="big long">Newfoundland</p>
Then you can use this code:
$(function () {
var size = '',
coat = '',
$list = $('#list'),
selectType = function () {
$list.find('p').hide();
$list.find('p' + size + coat).show();
};
$("#big,#small").click(function() {
size = '.' + this.id;
$('#big').toggleClass('selected', this.id === "big");
$('#small').toggleClass('selected', this.id === "small");
selectType();
});
$("#short,#long").click(function () {
coat = '.' + this.id;
$('#short').toggleClass('selected', this.id === "short");
$('#long').toggleClass('selected', this.id === "long");
selectType();
});
});
Opps, updated the code to work properly and color the selected button.

Related

jQuery target a specific div with another var

So, I have the following php and js markup:
<div class="top" data-id="<?php echo $id; ?>">
<div class="middle">
Click
</div>
<div class="info"> <!--Note that I am targeting a child div -->
<div class="name">
Steve
</div>
</div>
</div>
Then for my js:
jQuery(document).on( 'click', '.middle', function(e) {
var my_id= jQuery(this).parent('.top').data("id");
});
At this point, when the middle div is clicked, the js var my_id is equal to the data_id of its top parent div (with .top).
Now, I want to save another div as below (simple html).
var my_name = $('.name').html();
How do I target the specific .name class within the specific `data-id' div?
You could use the .siblings() method to select all the sibling elements of .middle, and then use .find() to find any descendant .name elements:
jQuery(document).on( 'click', '.middle', function(e) {
var my_id= jQuery(this).parent('.top').data("id");
var my_name = jQuery(this).siblings().find('.name').html();
});
Use the parent and find methods.
var current;
$(document).on( 'click', '.middle', function(e) {
var my_id= $(this).parent('.top').data("id");
current = $(this).parent().find('.name');
console.log($(current).html());
});
Edit: John Crozier's answer is more complete. Beat me to it :)

jQuery Parents and Data

So, I have the following php:
<div class="top" data-id="<?php echo $id; ?>">
<div class="middle">
Click
</div>
</div>
Then for my js:
jQuery(document).on( 'click', '.middle', function(e) {
var my_id= jQuery(this).data("id"); ???
}
So at the current setup, when the '.middle' class is clicked, then I want to target the ".top" class and save the data-id.
I can use jQuery(this).parents('.top'); but I am not sure how to combine them together.
My question is, how do I target the parents (top) then save the data-id variable?
Thanks
You can use .parent() and chain it for other function invocations.
jQuery(document).on( 'click', '.middle', function(e) {
var my_id= jQuery(this).parent(".top").data("id");
});
And in the place of document, try to use any closest static parent of .middle
$(function() {
$('.middle').on('click', function() {
var id = $(this).closest('.top').data('id');
});
});

get data on mousehover for different id

I have a listing of products each with differnt ID. Now on frontend I want to get prodouct data(say, name,price and a addtocart button) on mousover.
Here is my code:
This is in loop to get all products:
HTML:
<div class="prod">
<a class="product-image pi_470" title="Cushion Tsavorites" href="/tsavorite/cushion-tsavorites-1328.html"><img height="135" width="135" alt="Cushion Tsavorites" src="/small_image.jpg"></a>
<div style="display: none; margin: -65px 0px 0px 5px; position: absolute; z-index: 30;" class="mouse_hover_470">
<input type="hidden" id="prod_id" value="470">
<h2 class="product-name"><a title="Cushion Tsavorites" href="/tsavorite/cushion-tsavorites-1328.html">Cushion Tsavorites</a></h2>
<div class="price-box">
<span id="product-price-470" class="regular-price">
<span class="price">$387.15</span>
</span>
</div>
<div class="actions">
<button onclick="setLocation('http://dev614.trigma.us/chocolate/index.php/checkout/cart/add/uenc/aHR0cDovL2RldjYxNC50cmlnbWEudXMvY2hvY29sYXRlL2luZGV4LnBocC90c2F2b3JpdGUuaHRtbA,,/product/470/form_key/4BR7w0TqeeO9AC0g/')" class="button btn-cart" title="Add to Cart" type="button"><span><span>Add to Cart</span></span></button>
</div>
</div>
</div>
jQuery:
jQuery(document).ready(function() {
var bla = jQuery('#prod_id').val();
jQuery(".pi_" + bla).mouseover(function() {
//alert("hello");
jQuery(".mouse_hover_" + bla).css("display", "block");
});
jQuery(".pi_" + bla).mouseout(function() {
jQuery(".mouse_hover_" + bla).css("display", "none");
});
});
But Iam getting only data of first product on mouseover. Its not working for rest of products
Looks like you are executing the above block of code in a loop, once per each product. In that case the problem is jQuery('#prod_id').val(); it will always return the value of first element with id prod_id.
In your case you don't have to do that, you can
jQuery(function ($) {
$('.prod .product-image').hover(function () {
$(this).next().show();
}, function () {
$(this).next().hide();
})
});
There is a much, much easier way to do this:
jQuery(document).ready(function() {
jQuery(".product-image").hover(function() {
$(this).next().show();
}, function() {
$(this).next().hide();
});
});
Demo: JSBin
You can use each() function in jQuery
NOTE: Instead of using id="prod_id", use class, i.e class="prod_id". Since you told that the div is dynamically created it is using the same id attribute
Now loop the product div on ready function
jQuery(document).ready(function() {
jQuery('.prod').each(function(){
var bla = jQuery('.prod_id').val();
jQuery(".pi_" + bla).on('mouseover',function() {
//alert("hello");
jQuery(".mouse_hover_" + bla).css("display", "block");
});
jQuery(".pi_" + bla).on('mouseout',function() {
jQuery(".mouse_hover_" + bla).css("display", "none");
});
});
});
You can checkout this jQuery each()
Ashi,
try using
var bla = jQuery(input[id*='prod_id']).val();
instead of
var bla = jQuery('#prod_id').val();
This will give you all the hidden inputs so loop all of them and bind the mouseover event.
For example:
jQuery(input[id*='prod_id']).each(function(){
var bla = jQuery(this).val();
//carry out your logic..
// you can use jquery().live('mouseover'function(){}) for dynamically created html
});
Hope this will work!!
Cheers!!
function handler(ev) {
var target = $(ev.target);
var elId = target.attr('id');
if( target.is(".el") ) {
alert('The mouse was over'+ elId );
}
}
$(".el").mouseleave(handler);
http://jsfiddle.net/roXon/dJgf4/

Chaging background color on hover using foreach

I am using this code (smarty php template code)
{foreach $rewards as $reward}
<div id="reward" data-id="{$reward['id']}">
<div class="reward_data{$reward['id']}">
<b>{strtoupper($reward['title'])}:</b><br/>
{html_entity_decode($reward['description'])}
<br/>
<br/>
<b>Estimated Delivery:</b> {$reward['estimated_delivery_date']}
<br/>
<br/>
<button class="btn btn-danger">Select This Project</button>
</div>
</div>
<br/>
<br/>
<br/>
{/foreach}
and in jquery i am using
$(document).ready(function () {
$('#reward').hover(function () {
var reward_id = $(this).attr('data-id');
$('.reward_data' + reward_id).css("background-color", "#2bde73");
$('.reward_data' + reward_id).css("padding", "10px");
$('.reward_data' + reward_id).css("border-radius", "10px");
}, function () {
var reward_id = $(this).attr('data-id');
$('.reward_data' + reward_id).css("background-color", "#ffffff");
});
});
On hover ist result from foreach the background color is changed but on 2nd and above results there is effect of on hover. Please help me related this
I am thankful to you
For simply changing the background color on hover, CSS is the tool you need. First of all, you need to use class instead of id as ids should be unique.
So
<div id="reward" data-id="{$reward['id']}">
Becomes
<div class="reward" data-id="{$reward['id']}">
Then create a new CSS rule as follows
.reward:hover .reward-data {
background-color: #2bde73;
padding: 10px;
border-radius: 10px;
}
No need for JS and jQuery.
See this: Can multiple different HTML elements have the same ID if they're different elements?
If you know that an ID needs to be unique, you'll see that this won't work: you have the ID "reward" multiple times.
A solution could be that you change the ID to a class, and change the hash to a dot in your jquery.
change your id to class:
{foreach $rewards as $reward}
<div class="reward" data-id="{$reward['id']}">
<div class="reward_data{$reward['id']}">
<b>{strtoupper($reward['title'])}:</b><br/>
{html_entity_decode($reward['description'])}
<br/>
<br/>
<b>Estimated Delivery:</b> {$reward['estimated_delivery_date']}
<br/>
<br/>
<button class="btn btn-danger">Select This Project</button>
</div>
</div>
<br/>
<br/>
<br/>
{/foreach}
And jquery as below:
$(document).ready(function () {
$('.reward').hover(function () {
var reward_id = $(this).attr('data-id');
$('.reward_data' + reward_id).css("background-color", "#2bde73");
$('.reward_data' + reward_id).css("padding", "10px");
$('.reward_data' + reward_id).css("border-radius", "10px");
}, function () {
var reward_id = $(this).attr('data-id');
$('.reward_data' + reward_id).css("background-color", "#ffffff");
});
});

how to show repeating divs on mouse over with jquery?

for example ive got a div like:
<div class="y1">
<img src="i/o.png" />
</div>
and
<!-- pre load -->
<div class="p1" style="display:none">
<h5 class="ob">Title</h5>
<img class="ob" src="i/ob.png" />
<small class="ob">Description</small>
PLAY
</div>
and this jquery
<script type="text/javascript">
$("div.y1").hover(
function () {
$('div.p1').slideDown('slow', function() {
});
}
);
</script>
my question is how can i repeat it for 12 times. i mean when i hover on y1, show p1, y2 => p2, y3 => p3 ... y12 => p12. i hope you guys understand me. thank you so much!!!!
Should look like:
$(function(){
$('div[class^=y]').hover(function(){
var self = $(this),
number = this.className.slice(1);
$('div.p' + number).slideDown('slow', function() {
});
}, function() {
var self = $(this),
number = this.className.slice(1);
$('div.p' + number).slideUp('slow', function() {
});
});
});
Example: http://www.jsfiddle.net/Q5Ug2/
I'm going to assume that your y# divs are inside a div with the id container. Secondly, I'm going to assume that none of your y# divs have any other classes applied to them.
$('#container').delegate('div[class^=y]','mouseenter', function(){
$('div.p' + this.className.slice(1)).slideDown('slow');
}).delegate('div[class^=y]', 'mouseleave', function(){
$('div.p' + this.className.slice(1)).slideUp('slow');
});
This uses delegate to avoid the cost of binding to multiple DOM elements.
Edit To hide the p# div on hovering on another element, you can use this code.
$('#container').delegate('div[class^=y]','mouseenter', function(){
$('div.p' + this.className.slice(1)).slideDown('slow').siblings().slideUp();
});
Do things a bit differently..
Use ID's for the p# and y# series indicators.
On your DIV tags for the p# series, add a title of the y# series.. so <div id="p1" title="y1">
On your DIV tags for the y# series, add a class.. <div id="y1" class="hoverMe">
$('div.hoverMe').hover( function() {
$('[title=' + $(this).attr('id') + ']').slideDown('slow');
});

Categories