I have this div tags :
<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 -->
and this JavaScript :
<script type="text/javascript">
$(document).ready(function($) {
$('ul li').click(function() {
$('input#shareto').val($(this).data('val'));
});
});
</script>
that JavaScript actually works when I'm using it with that div alone. but when I put it on my full code, that JavaScript doesn't work. I think that because there are more than one UL and LI tags on my code.
Now, the question is... how to apply that Javascript so that it can works ONLY for that div, even though there are other UL and LI tags.
Just use this instead targeting the div first then its contents
$('#target-share ul li')
JavaScript (not just Jquery) is all about scoping:
$('ul li').click(function() {
Causes that click to bind to every li in a ul so what you wanna do is scope the click down to a specific area of your page or set of elements across the page.
To scope it down to a specific element the best idea is to use a id like so:
$('#target-share ul li')
But to scope it down to a number of elements it is better to use a class like:
$('.target-share ul li')
Edit:
Also on() could be a good replacement here for click but it depends on where the HTML is being sourced and how it is being used, but thought I would make you aware of that function in case you didn't already know about it.
Select those ul li that have your data-val attribute:
$(function($) {
$('ul li[data-val]').click(function() {
$('input#shareto').val($(this).data('val'));
});
});
Related
In jquery, I need to dynamically display first two values and after while click remaining values should be display?
<div class="category_items">
<div class="category_item">
<div class="test">test1_1</div>
<div class="test">test1_2</div>
<div class="test">test1_3</div>
<div class="test">test1_4</div>
<div class="test">test1_5</div>
<div class="test">test1_6</div>
</div>
<button>Show / Hide</button>
<script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
<script>
var elems = $('.category_item', '.category_items').filter(function() {
return $(this).children().length > 2;
}).hide();
$('button').on('click', function() {
elems.toggle();
});
</script>
I want to display first two items by default when i click the button i want show full list
Do this with css with
HTML
<style>
li div{display:none;}
li:hover div {display:block;position:absolute;left:200px;width:100px;height:100px;background-color:red;}
</style>
<ul>
<li>123<div>1</div></li>
<li>222<div>2</div></li>
<li>333<div>3</div></li>
</ul>
enter code here
:gt selector will be good for this situation. I hope, this code helps you.
<script>
$('.category_item .test:gt(1)').hide();
$('button').on('click', function() {
$('.category_item .test:gt(1)').toggle();
});
</script>
If you want to change the number of elements which you want to show, just change number in gt() selector.
See this also but I not sure if is normal to use span as child of ul not li but you can put in last li another ul and when show this new ul.
This all li is show when zou hover ul bu zou can do to show all li onli when hove some think one as example + hover last show li(user no need to click to see al!).
<style>
ul span{display:none;}
ul:hover span {display:block;/*position:absolute;left:200px;background-color:red;*/}
</style>
<ul>
<li>123<div>1</div></li>
<li>22<div>2</div></li>
<span>
<li>223<div>3</div></li>
<li>223<div>3</div></li>
<li>223<div>3</div></li>
</span>
</ul>
I am trying to write a menu for a front page that when an item is hovered over, the background image of the background div changes.
I have three main documents creating this effect, menuFunction.php, which is included in the header file, so it is not a link problem, then there is index.php, and finally my stylesheet.
Here is an example of one of the menu items functions from my menuFunction.php file:
<script>
$(document).ready(function () {
$(".resume").hover(function () {
$(".backgroundImage").css("background", "url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuResume.jpg)");
});
$(".resume").mouseleave(function () {
$(".backgroundImage").removeAttr('style');
});
});
</script>
Now here is where it is mentioned in index.php:
<div id="photoBackground" class="backgroundImage">
<div id="menuWrap">
<ul id="menu">
<li id="menuResume" class="resume">Resume</li>
<li id="menuAbout" class="about">About</li>
<li id="menuGallery" class="gallery"><a href="http://www.vhawley.com/gallery">Gallery</li>
<li id="menuContact" class="contact"><a href="http://www.vhawley.com/contact">Contact</li>
</ul>
</div>
</div>
Notice that the classes mentioned in the menuFunctions.php are also mentioned in the corresponding li.
Now, I am able through my stylesheet to define that the background have the following attributes for #photoBackground
#photoBackground{
height:450px;
width:100%;
background-image:url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuDefault.jpg);
background-repeat:no-repeat;
background-position:center;
}
Lastly, what I am now trying to accomplish is to get the rest to work, but even when I use the class .resume in my CSS, it does not work, even when I use the div #menuResume, no luck. I have been trying forever and have tried putting off asking for help on this one, but I really am stuck. I have really no JQuery experience, just CSS, HTML 5, and some PHP, which usually if you can master them, they can make a decent site alone. Until you want to get fancy like this.
Any ideas?
EDIT:
It is an issue with linking the JQuery variables to CSS, it has nothing to do with getting the background to change, because as Popnoodles proved on JSFiddle, nothing is wrong with that.
You could do this with the css :hover selector. However, if you want to use jQuery, you need to change .css('background'... to .css('background-image'...
It is the small mistakes that trip us up the most.
Edit to clarify your mistake:
The background style overrides everything, including background-repeat and background-position so those are all gone. If you used background-image it wouldn't override those other css options.
<script>
$(document).ready(function () {
$(".resume").hover(function () {
$(".backgroundImage").css("background-image", "url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuResume.jpg)");
});
$(".resume").mouseleave(function () {
$(".backgroundImage").removeAttr('style');
});
});
</script>
Best way to do this would be plain css
.resume #photoBackground {
...
}
.resume:hover #photoBackground {
... new image
}
I would research spriting to save some image loads, but that would requeire a fixed or known hight for the background image container. I would also use the following instead of wiring up a handler for each menu item:
HTML
<div id="photoBackground" class="backgroundImage">
<div id="menuWrap">
<ul id="menu">
<li id="menuResume" class="resume">Resume
</li>
<li id="menuAbout" class="about">About
</li>
<li id="menuGallery" class="gallery">Gallery
</li>
<li id="menuContact" class="contact">Contact
</li>
</ul>
</div>
</div>
Script
//Save looking up background container each time
var background = $("#photoBackground");
$("#menu li").hover(
//Hanldes Mouse Enter
function () {
var cssClass = $(this).attr('class');
$(background).addClass(cssClass);
},
//Handles Mouse Exit
function () {
var cssClass = $(this).attr('class');
$(background).removeClass(cssClass);
})
CSS
.backgroundImage {
background-image:url(http://www.vhawley.com/wp-content/themes/vhawleycomtheme/css/style/images/menuResume.jpg);
}
.backgroundImage.resume {
background-position:left -100px;
}
.backgroundImage.about {
background-position:left -200px;
}
.backgroundImage.gallery {
background-position:left -300px;
}
.backgroundImage.contact {
background-position:left -400px;
}
This also kind of demonstrates the principle of spriting by just changing the offset of the background image. OF course you could always just change the background image itself.
Demo
I have the following HTML generated by PHP:
<input><label>anchor 139</label>
<div class="slidingDiv 139" style="display: none;">content 139</div>
anchor 140</label>
<div class="slidingDiv 140" style="display: none;">content 140</div>
<input><label>anchor 141</label>
<div class="slidingDiv 141" style="display: none;">content 141</div>
<input><label>anchor 142</label>
<div class="slidingDiv 142" style="display: none;">content 142</div>
As you can see it, the second class of anchor and div is the same for each iteration of the loop. I did like this because I am using a jquery snippet to hide/show div onclick on anchor.
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
});
</script>
The problem of this jquery code: It doesn't take into consideration the second class I am adding (to anchor and div). In fact I want to hide the specific div related to his anchor, not all the divs.
Thank you for your time and help.
Try using .next which seems to do the trick for your markup.
$(this).next('.slidingDiv').slideToggle();
Full Code:
<style>
.slidingDiv { display: none; }
</style>
<script>
$(document).ready(function(){
$('.show_hide').click(function(e){
e.preventDefault(); //to prevent default action of link tag
$(this).parent().next('.slidingDiv').slideToggle();
});
});
</script>
Note: I added display:none style rule to .slidingDiv so that it is hidden when the page is rendering and not after page is rendered.
Try this using next:
$('.show_hide').click(function () {
$(this).next(".slidingDiv").slideToggle();
});
Your code:
$(".slidingDiv").slideToggle();
was sliding all the div with class slidingDiv. But we need to take into consideration the div next to the element being clicked in the current scope only using the next() method.
I asign class to element but after loading page the class is removed.
I use this function in my script.js
$('#main-nav li ul li a').click(function(){
$('#main-nav li ul li').children().removeClass('current');
$(this).addClass('current');
});
and this in my PHP view:
<ul id="main-nav"> <!-- Accordion Menu -->
<li>
<a href="#" class="nav-top-item no-submenu"> <!-- Add the class "no-submenu" to menu items with no sub menu -->
On site
</a>
</li>
<li>
<a href="#" class="nav-top-item current"> <!-- Add the class "current" to current menu item -->
Programs
</a>
<ul>
<li>Manage country</li>
<li>Manage channel</li>
<li>Manage Comments</li>
<li>Manage Categories</li>
</ul>
</li>
</ul> <!-- End #main-nav -->
If I use this <li>Manage</li> the class current is added but after load is removed.
How can I add class if I use php code after load page or have you any solution ?
jQuery is scripting language ... When loading the page it will definitely remove your previous functionality as click event doesn't fired so it won't work....
After loading the page just put some logic through PHP ....
with JQuery you can execute code when page is loaded :
$(document).ready(function(){
// your code here
});
this should work:
$(document).ready(function(){
$('#main-nav li > a').click(function(e){
$('#main-nav a').removeClass('current');
$(e.target).addClass('current');
});
});
if it doesn't work, try this block of script before </body> at the end of your document:
<script type="text/javascript">
$(function(){
$('#main-nav li > a').click(function(e){
$('#main-nav a').removeClass('current');
$(e.target).addClass('current');
});
});
</script>
this will force the execution after all the html is rendered by the php
First of all wrap your function in a $(document). ready(function () {}) and use the $('#main-nav'). on('click', 'li > a', function () {} ) approach inside of it. . on() is better to attach event listener to an dynamically loaded elements.
How I can do a foreach or similar in jQuery? I need that when the user clicks in the <span>, add the class inactive to all <div>s with the vote active class in the <ul>.
I've tried with the .each method, but it doesn't work.
I have this HTML:
<ul class="suggestions inactive">
<li id="s2">
<div class="id">2</div>
<div class="vote inactive">
<span class="up"></span>
<span class="down"></span>
</div>
<div class="text">test2</div>
<div class="rating">2</div>
</li>
<li id="s3">
<div class="id">3</div>
<div class="vote active">
<span class="up"></span>
<span class="down"></span>
</div>
<div class="text">test3</div>
<div class="rating">0</div>
</li>
</ul>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="script.js"></script>
And this script:
$(document).ready(function(){
var ul = $('ul.suggestions');
// Listening of a click on a UP or DOWN arrow:
$('div.vote span').live('click',function(){
var elem = $(this),
parent = elem.parent(),
li = elem.closest('li'),
ratingDiv = li.find('.rating'),
id = li.attr('id').replace('s',''),
v = 1,
ul2 = elem.closest('li').parent();
// If the user's already voted:
if(parent.hasClass('inactive')){
return false;
}
if(ul2.hasClass('inactive')){
return false;
}
//If the user's already voted, add following class
parent.removeClass('active').addClass('inactive');
ul2.removeClass('active').addClass('inactive');
if(elem.hasClass('down')){
v = -1;
}
// Incrementing the counter on the right:
ratingDiv.text(v + +ratingDiv.text());
// Turning all the LI elements into an array
// and sorting it on the number of votes:
var arr = $.makeArray(ul.find('li')).sort(function(l,r){
return +$('.rating',r).text() - +$('.rating',l).text();
});
// Adding the sorted LIs to the UL
ul.html(arr);
// Sending an AJAX request
$.get('ajax.php',{action:'vote',vote:v,'id':id});
});
You don't need .each() here at all.
Why? Because jQuery's selectors wonderfully select everything of a single selector type. $('p') selects every p in your HTML.
You should use
$(this).parent().addClass('active').removeClass('inactive');`
from within the scope of the
$('div.vote span').live('click',function() { ... } );
Also, remember that .live() is deprecated in jQuery 1.7+.
Are you sure you really want to add that class to ALL elements in the HTML? If so, I guess I would just do this:
$("div").addClass("inactive");
You do not need each for adding class to multiple elements. Just select them and apply addClass():
jQuery('div.vote.active').addClass('inactive');
If you insist on using .each(), then it really works. Just use it properly:
jQuery('div.vote.active').each(function(){
jQuery(this).addClass('inactive');
});
Did it help?