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.
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>
<div class="a">hello</div>
<div class="b">bye</div>
I have a one page website and I want a div to be loaded first and then the second one and... Can I do this just with html and css? Or it needs JavaScript or...
Just do it with Javascript:
Change your Body to <body onload="onloadFunction()">
Add style="display: none;" to your div Elements
Add following script:
<script language="javascript" type="text/javascript">
function onloadFunction(){
document.getElementsByClassName("a").style.display = "true";
document.getElementsByClassName("b").style.display = "true";
}
</script>
and change the order of the document. lines to which order you want to have it.
You can try this with something like.
$(document).ready(function() {
$(".b").delay(2000).fadeIn(500); //class b is on delay
});
Can't done with only HTML and CSS. Need to involve JavaScript or jQuery code too.
Yes you need javascript and I suggest jquery(javascript library).
More info at: http://www.w3schools.com/jquery/jquery_get_started.asp
And here is working example(just run with browser and click button):
<div class="a" style="display: none;">hello</div>
<div class="b" style="display: none;">bye</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.a').show();
setTimeout(function(){$('.b').show()}, 2000);
});
</script>
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'));
});
});
Is it possible to use jquery to use a callback in a div and show the full text in another div? Currently I have in the right div:
$(document).ready(function(){
setInterval(function(){
$.post("status.php?_="+ $.now(), {day : "<?php echo $day;?>"}, function(upcoming){
$(".ticker").html(upcoming);
}),
"html"
}, 45000);
});
And I need something like:
$(".view").hover(function(){
$("#left").load("a.php");
});
<div id="left">
<div class="show">
</div>
</div>
<div id="right">
<div class="ticker">
</div>
</div>
My php spits back html: EventID
What I want to do is hover over the hyper link and have the full status shown in .show
I know I'll have to do a query with the event number, but my search in doing this comes up empty. As always, any help is appreciated
$("body").on("hover", ".view", function(){
$("#left").load("a.php");
});
As you're adding .view to DOM after page load i.e dynamically so, you need delegate event handler for dynamic element using .on().
And instead of body you can use any parent selector of .view which is static-element.
How do I put this together? I'm working on this website that requires that every time that a item from the menu is clicked the background-position of two specific DIVs change and keep the selection.
Here is my attempt:
First I create a variable "p" to save on the url/address the selected page, so for example, if you are in about us you will probably see something like this on the address bar:
website.com/index.php?p=aboutus
On the menu, on the button about us I did this with a call to the function changeBG onClick:
About Us
I figured I need javascript to do the trick and here is where I'm stock.
This is the script that goes in the header:
<script type="text/javascript">
<?
// Set "p" value to HOME if previously empty.
if ($_GET['p']=='') $_GET['p']='home';
?>
function changeBg(pvalue) {
if (pvalue == '') {pvalue = '<?=$_GET['p'];?>';
}
if (pvalue=='aboutus'){
document.getElementById('this_is_the_first_to_be_changed').style.backgroundPosition=0 20px;
document.getElementById('this_is_the_other_to_be_changed').style.backgroundPosition=0 80px;
if (pvalue=='contactus'){
document.getElementById('this_is_the_first_to_be_changed').style.backgroundPosition=0 10px;
document.getElementById('this_is_the_other_to_be_changed').style.backgroundPosition=0 100px;
}
}
</script>
This is what goes on the body:
<div id="left_menu">
About Us
Contact Us
</div>
<div id="this_is_the_first_to_be_changed">
</div>
<div id="this_is_the_other_to_be_changed">
</div>
Can anyone spot why this doesn't work?
Try it using quotes:
style.backgroundPosition="0 20px";
UPDATED
Also, what about this line:
pvalue = '';
you are setting pvalue='=aboutus'
UPDATED
The script element is incorrect:
<script type="java/text">
right is:
<script type="text/javascript">
In addition use firebug or chrome to see the JS generated in the page.