How to use jQuery slideDown() to display _almost_ all contents? - javascript

I have a div within a div. On page load, they should both be hidden, then when I trigger the slideDown() function on the outer div, I want the inner div to remain hidden. How can I achieve this?
<script>
$(function(){
$('.body').hide();
$('.display').click(function(){
$(this).closest('.wrapper').find('.body').slideDown();
});
});
</script>
<div class="wrapper">
<a class="display" href="#">Display Outer</a>
<div class="body">
Now displaying outer div
<div class="wrapper">
<a class="display" href="#">Display Inner</a>
<div class="body">
Now displaying inner div
</div>
</div>
</div>
</div>
Here is an example of it not working: http://jsfiddle.net/b7Tpt/

The reason it doesn't work is the use of find. find would traverse all levels to find the matches while the children would travel single level. So use find('.body:first') or children('.body')
$(function(){
$('.body').hide();
$('.display').click(function(){
$(this).closest('.wrapper').find('.body:first').slideDown();
});
});
Updated Example
OR
$(function(){
$('.body').hide();
$('.display').click(function(){
$(this).closest('.wrapper').children('.body').slideDown();
});
});
Updated Example

Try -
$('.display').click(function(){
$(this).siblings('.body').slideDown();
});
I think $(this).closest('.wrapper') was moving up the DOM tree and finding the top most wrapper div then opening all the body classes it found underneath. Using siblings should get the element with a body class that is directly beneath the clicked link.
Demo - http://jsfiddle.net/pMgVj/1/

try this: http://jsfiddle.net/Kf6gk/

Related

Hide/Show specific divs with JavaScript

I want to make an HTML element reference like this
<html> X
- description -
<head> X
- description -
<body> X
- description -
The description of the element is hidden by default so when I click on the "X" or the DIV that contains that element description, the description shows up or hide when I click it again.
This is my HTML code:
<h1>HTML Reference</h1>
<div id="reference-list">
<div class="list-element"><html></div>
<div class="element-desc">Here's go description</div>
<div class="list-element"><head></div>
<div class="element-desc">Here's go description</div>
</div>
I tried with jQuery but I dont know how to select the specific div containing the description of that element, since all the divs have the same class (otherwise I will have to create 100 class for each HTML element).
What I tried is this:
<script>
$(document).ready(function(){
$(".list-element").click(function(){
$(".element-desc").toggle();
});
});
</script>
This doesn't work since it show/hide the description of all the elements on the site.
You can use .next() if thats is the correct position of your element.
$(document).ready(function(){
$(".list-element").click(function(){
$(this).next().toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>HTML Reference</h1>
<div id="reference-list">
<div class="list-element"><html></div>
<div class="element-desc">Here's go description</div>
<div class="list-element"><head></div>
<div class="element-desc">Here's go description</div>
</div>
You're selecting all the element descriptions with the $(".element-desc") selector. Try doing this instead:
$(document).ready(function(){
$(".list-element").click(function(){
$(this).next().toggle();
});
});
Using next selects the next element. Basically you want to make your selector more specific and using next is one example of how to do so

How to toggle specific divs with jquery

Okay so I have a very limited amount of knowledge with this and I can not find my answer anywhere. What I am trying to do is create multiple buttons that toggle information. So when the first toggle is clicked div 1 is toggled, when i click the second toggle div two opens and preferably div 1 closes. My code is very basic I am very new to this. Right now no matter what values I input into the toggle area both divs close. Thank you and I hope this makes sense.
Here is my code:
<script>
$(document).ready(function(){
$("button").click(function(){
$("div.house").toggle();
});
});
</script>
<button>Toggle</button>
<div class="house">
<p>SAMPLE TEXT ETC...</p>
</div>
<button>Toggle</button>
<div class="tumble-by">
<p>SAMPLE TEXT ETC...</p>
</div>
You can select the next sibling:
$("button").click(function(){
$(this).next().toggle();
});
In the above code, JavaScript this keyword refers to the clicked element. $(this) creates a jQuery collection and .next() method selects the very next sibling of the collection's element.
I agree too, that first you need to hide all divs:
$("button").click(function () {
$('div').hide();
$(this).next().toggle();
});
<script>
$(document).ready(function () {
$(document).on("click", ".js-toggle__button", function (e) {
$(".js-toggle__text").hide();
$(this).next(".js-toggle__text").show();
});
});
</script>
<button class="toggle__button js-toggle__button">Toggle</button>
<div class="toggle__text js-toggle__text">
<p>SAMPLE TEXT 1 ETC...</p>
</div>
<button class="toggle__button js-toggle__button">Toggle</button>
<div class="toggle__text js-toggle__text">
<p>SAMPLE TEXT 2 ETC...</p>
</div>
It's better to use uniquely defined identifiers when you accessing elements from JS (and don't use them for CSS — use separate names).
Your HTML code some day can be changed dramatically and JS will work anyway because it depends on identifiers but not on structure or on tag names.

Javascript dom selection of next element

This is roughly my setup:
<div class="wrapper">
<div class="first">
<a class="button" href="">click</a>
</div>
<div class="second">
<div class="third">
Stuff
<div>
</div>
</div>
Ok, so what I want to is this: when you click the a tag, the .third div should animate.
What I have so far is this:
button.click ->
third.animate
left: '+=100%'
The problem is, I have multiple of these wrappers on one page. So when I click the button, every '.third' div on the page animates. How can I select the right one and only that one?
Thanks!
Try this:
$('a').click(function(){
var third = $(this).closest('.wrapper').find('.third');
//use third variable to animate
});
You can use closest or parents.
If you want only one div to animate, assign an id to the div and animate only that one by$("#third").animate("left", "100%");

jQuery on hover show for multiple elements

I have a page that contains multiple lines like this each wrapped within <div id="result">;
<div id="result">Link Name<iframe src="http://www.domain.com/" style="width:285px;height:285px;border:0px;margin:0px" scrolling="no"></iframe></div>
I am currently using the following jQuery to display the <a> tag on hover;
$(document).ready(function(){
$('#result iframe').hover(function(){
$('#result a').fadeIn(200);
},function(){
$('#result a').fadeOut(200);
});
});
However, the hover only works on the first <div id="result"> and also shows the <a> tags for every <div id="result"> rather than just the one the user hovered on.
How can I fix this?
You can try this - Changing results to a class
$(document).ready(function(){
$('.result').hover(function(){ // <-- change to class.. and bind to wrapper div
$(this).find('a').fadeIn(200);
},function(){
$(this).find('a').fadeOut(200);
});
});
Assuming I understand your weird thing :
Html
<div class="result">
Link Name
<iframe src="http://www.domain.com/" style="width:285px;height:285px;border:0px;margin:0px;background:red;" scrolling="no"></iframe>
</div>
<div class="result">
Link Name
<iframe src="http://www.domain.com/" style="width:285px;height:285px;border:0px;margin:0px;background:red;" scrolling="no"></iframe>
</div>
jQuery
$('.result iframe').hover(function(e) {
$(this).parent().find('a').fadeIn();
}, function() {
$(this).parent().find('a').fadeOut();
});
See fiddle
Edit with hover.
Nb: e.preventDefault(); on click event if you don't want the link to submit by clicking.
Try it like this:
$(document).ready(function(){
$('#result iframe').hover(function(){
$('#result a').fadeIn(200);
$('#result a').fadeOut(200);
});
});
If you want to catch only the <a> tags not for every, but for a specific <div id="result">, you can try to specify that in your jQuery code, for example:
$(document).ready(function(){
$('#result:nth-child(1) iframe').hover(function(){
$('#result:nth-child(1) a').fadeIn(200);
},function(){
$('#result:nth-child(1) a').fadeOut(200);
});
});
So this will target only the first div with id="result". Catch the others by changing nth-child(0) - nth-child(1) - nth-child(2) ...
Another solution:
You can also set an id for every <a> tag or also, you can use a class to catch the specific element you need.

JQuery closest() help

So I have several containers with this markup on a page:
<div class="box w400">
<div class="box-header">
<span class="expand-collapse">expand/collapse</span>
<h3>Heading</h3>
</div>
<div class="box-content">
<p>Some content here...</p>
</div>
</div>
And I am trying to achieve that after clicking on the .expand-collapse span, the .box-content div will slide up or down (toggle).
Here is the jQuery I'm using, I am trying to achieve this with closest():
$(document).ready(function() {
$('.expand-collapse').click(function() {
$(this).closest('.box-content').slideToggle('slow');
});
});
But it is not working for some reason :(
Try this:
$(document).ready(function() {
$('.expand-collapse').click(function() {
$(this).parent().next('.box-content').slideToggle('slow');
});
});
That selects the next sibling of the parent div, it does not make use of closest.
closest() finds the closes parent element. In your case, the span doesn't have any parent elements with class .box-content. why not just do $('.box-content').slideToggle('slow'); ??
edit: i missed the part where you have several of these on a page. the parent().next should work.

Categories