Hide all video divs when one is selected - javascript

I'm a newbie with Javascript and having a hard time figuring out the correct code for this function.
I have multiple divs. Each div has 2 child divs that swap when clicked.
For instance, the first div is a styled image with a rollover effect. When the image is clicked, it swaps with a <video>.
However, I'm trying to add some functionality to the this... If I click on another image to play the <video>, I want the other videos to stop and swap back to the original image divs.
Here's the code I'm testing with.
function SwapDivsWithClick(div1, div2) {
d1 = document.getElementById(div1);
d2 = document.getElementById(div2);
if (d2.style.display == "none") {
d1.style.display = "none";
d2.style.display = "block";
} else {
d1.style.display = "block";
d2.style.display = "none";
}
}
<div id="project1Img" class="showDiv" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
Img1
</p>
</div>
<div id="project1Vid" class="hideDiv" style="display:none; border:2px dotted blue; padding:25px;">
<video>video content</video>
</div>
<div id="project2Img" class="showDiv" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
Img2
</p>
</div>
<div id="project2Vid" class="hideDiv" style="display:none; border:2px dotted blue; padding:25px;">
<video>video content</video>
</div>
<div id="project3Img" class="showDiv" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
Img3
</p>
</div>
<div id="project3Vid" class="hideDiv" style="display:none; border:2px dotted blue; padding:25px;">
<video>video content</video>
</div>
The problem I'm trying to solve is that when you click a second link, then the other two revert back to the original div1.

Are you open to using jQuery or another DOM manipulation library? You can write something to solve this problem relatively easily if the weight of the file isn't a problem. In any case, if you use native JS, you might want to focus your energy on the ability to loop thru DOM elements.
Here is an easy version in jQuery
$('.toggler').on('click', function(e) {
e.preventDefault();
var thisProjectVideo = $(this).parents('.projectImg').siblings('.projectVid');
var thisProjectImg = $(this).parents('.projectImg');
var allProjectVids = $('.projectVid');
var allprojectImgs = $('.projectImg');
allprojectImgs.removeClass('displayNone');
thisProjectImg.addClass('displayNone');
allProjectVids.addClass('displayNone');
thisProjectVideo.removeClass('displayNone');
})
.displayNone {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="videoContainer">
<div id="project1Img" class="projectImg showDiv" style="border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
<a class="toggler">Img1</a>
</p>
</div>
<div id="project1Vid" class="projectVid hideDiv displayNone" style="border:2px dotted blue; padding:25px;">
<video>video content</video>
</div>
</div>
<div class="videoContainer">
<div id="project2Img" class="projectImg showDiv" style="border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
<a class="toggler">Img2</a>
</p>
</div>
<div id="project2Vid" class="projectVid hideDiv displayNone" style="border:2px dotted blue; padding:25px;">
<video>video content</video>
</div>
</div>
<div class="videoContainer">
<div id="project3Img" class="projectImg showDiv" style="border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
<a class="toggler">Img3</a>
</p>
</div>
<div id="project3Vid" class="projectVid hideDiv displayNone" style="border:2px dotted blue; padding:25px;">
<video>video content</video>
</div>
</div>

How about using document.getElementsByClassName('showDiv') and loop through them (skipping to the next iteration of the loop if the element id matches the div1 argument), and set style.display='block' on all of them. This will show all the images except the one with the id of div1.
Then use document.getElementsByClassName('hideDiv') and loop through them (again, skipping to the next one if the element id matches div2), and set style.display='none'. This will hide all the videos except the id of div2.
This will reset all the divs in your list (regardless of how many there are) to their default state, and then you can hide div1 and show div2, and you're done.
This replacement function will do exactly what you want:
function SwapDivsWithClick(div1, div2) {
var d = document.getElementsByClassName('showDiv');
for (i = 0; i < d.length; ++i) {
d[i].style.display = (d[i].id == div1) ? 'none' : 'block';
}
d = document.getElementsByClassName('hideDiv');
for (i = 0; i < d.length; ++i) {
d[i].style.display = (d[i].id == div2) ? 'block' : 'none';
}
}

Related

Reorder divs after ajax call using data-attribute and using .animate

I have a list of divs with unique numeric id and data-position in order as they are (1 for the first in the list, 2 for the second ...).
After doing a ajax call through jquery, the result is a JSON call that changes the order of those id's.
each div have a button where you can mark and unmark the div on the top of this list.
if the mark button is called, the ajax calls that id with a new position (generally 1, but if there are more messages marked as important, this may be 2 or 3 but always following the position order) from his unmarked position
if the unmark button is fired, the ajax calls his new position.
so more or less the result is this one:
before ajax call:
<div class="card-wrapper" style="display:block;" id="1563" data-position="1">
message text
</div>
<div class="card-wrapper" style="display:block;" id="1561" data-position="2">
message text
</div>
<div class="card-wrapper" style="display:block;" id="1567" data-position="3">
message text
</div>
<div class="card-wrapper" style="display:block;" id="1568" data-position="4">
message text
</div>
button is fired, ajax called:
if (res.response === 'ok') {
res.order.forEach(function (value) {
$('#' + value.id + '.card-wrapper').attr('data-position', value.position);
});
...
}
after ajax call:
<div class="card-wrapper" style="display:block;" id="1563" data-position="3">
message text
</div>
<div class="card-wrapper" style="display:block;" id="1561" data-position="1">
message text
</div>
<div class="card-wrapper" style="display:block;" id="1567" data-position="2">
message text
</div>
<div class="card-wrapper" style="display:block;" id="1568" data-position="4">
message text
</div>
now i would like to sort the divs with the new data position in order using some sort of animation (swapping them) just after the ajax has been called.
please consider that all .card-wrapper may have different height and inside of it there are different bootstrap cards.
i tried using .animate with jquery and more or less i got how it works (get the height of every div and use .animate({top: '+=x'}) or .animate({top: '-=x'}) but i can't figure out how to do it all together,
any help is appreciated.
sorry for any mispelling or typo.
if it's unclear just ask me.
thank you
This is what I understood as your requirement. You want to rearrange the divs by data-position after ajax call completion. I could not get a great animation but could get them to order properly using jquery. I put all the divs in another div with id called 'parent'.
Hope this helps.
<div class="container">
<h1>Div Rearrangement</h1>
<div class="row" id="parent">
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid yellow;" id="1568" data-position="5">
5 message text
</div>
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid blue;" id="1563" data-position="3">
3 message text
</div>
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid violet;" id="1561" data-position="1">
1 message text
</div>
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid orange;" id="1568" data-position="6">
6 message text
</div>
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid indigo;" id="1567" data-position="2">
2 message text
</div>
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid green;" id="1568" data-position="4">
4 message text
</div>
<div class="card-wrapper" style="display:block; height:100px; border: 1px solid red;" id="1568" data-position="7">
7 message text
</div>
</div>
</div>
<script>
if (res.response === 'ok') {
var count = $("#parent > div").length;
console.log(count);
for(var i=1; i<count+1; i++) {
var thisDiv = $(' #parent > div[data-position=' + i + ']');
console.log(thisDiv);
thisDiv.appendTo($("#parent")).hide().fadeIn(1000);
}
}
</script>

How to swap two div respectively on button click using jquery?

I want to swap div on each click. But it swaps on only first click.
function SwapDivsWithClick() {
$('#swapper-other').each(function() {
if (!$(this).text().match(/^\s*$/)) {
$(this).insertBefore($(this).prev("#swapper-first"));
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="swapper-first" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
This div displayed when the web page first loaded.
</p>
</div>
<div id="swapper-other" style="display:block; border:2px dotted blue; padding:25px;">
<p style="margin:0; color:blue;">
This div displayed when the link was clicked.
</p>
</div>
<p style="text-align:center; font-weight:bold; font-style:italic;">
(Swap Divs)
</p>
You doesn't need to use .each() because your selector contain one element. So use bottom code.
Select #swapper-first and #swapper-other in one selector and get which one that is first using .first() and use .before() to inserting selected element before another.
function SwapDivsWithClick() {
$('#swapper-first, #swapper-other').first().before(function(){
return $(this).next();
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="swapper-first" class="" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
This div displayed when the web page first loaded.
</p>
</div>
<div id="swapper-other" style="display:block; border:2px dotted blue; padding:25px;">
<p style="margin:0; color:blue;">
This div displayed when the link was clicked.
</p>
</div>
<p style="text-align:center; font-weight:bold; font-style:italic;">
(Swap Divs)
</p>
Here is a working example: the last div with the class swapIt is set before the first div with the class swapIt.
What I've done:
I gave every div the class swapIt. In the function the last element (:last) with this class is getting pushed over the first element with the class (swapIt).
function SwapDivsWithClick() {
$('.swapIt:last').insertBefore($('.swapIt:first'));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="swapper-first" class="swapIt" style="display:block; border:2px dashed red; padding:25px;">
<p style="margin:0; color:red;">
This div displayed when the web page first loaded.
</p>
</div>
<div id="swapper-other" class="swapIt" style="display:block; border:2px dotted blue; padding:25px;">
<p style="margin:0; color:blue;">
This div displayed when the link was clicked.
</p>
</div>
<p style="text-align:center; font-weight:bold; font-style:italic;">
(Swap Divs)
</p>
The approach with classes has the advantage that you can have more than two divs and every time the button is clicked the last div gets pushed over the first. Even with 5, 7 or 10 this would still work!

Moving <div> from one <div> to another back and forth

I found lots of information on the web about how to remove, attach, prepend, etc. Element from and to DIV. Unfortunately I have a hard time making it work.
In my project, I will have to attach element to div and move them around on a grid (each grid cell being a div of it's own) Bellow is a small script which mimics what I'm trying to do.
Here's the example (here's a link to JSFiddle : http://jsfiddle.net/mgR5b/25/):
HTML:
<div id="LeftAndRightRow1" style="display:inline-block; height:100%;width:100%;border:1px solid black; background-color: red;">
<div id="Left" style="display:inline-block; height:100%;width:45%; border:1px solid black; background-color: blue;">
<button id="B1">B1</button>
</div>
<div id="Right" style="display:inline-block; height:100%;width:45%;border:1px solid black; background-color: pink;">
<button id="B2">B2</button>
</div>
</div>
<div id="LeftAndRightRow2" style="display:inline-block; height:100%;width:100%;border:1px solid black; background-color: red;">
<div id="Left2" style="display:inline-block; height:100%;width:45%; border:1px solid black; background-color: yellow;">
<button id="B3">B3</button>
</div>
<div id="Right2" style="display:inline-block; height:100%;width:45%;border:1px solid black; background-color: purple;">
<button id="B4">B4</button>
</div>
</div>
<br>
<button id="SwapButton">Swap Button</button>
JQuery / JavaScript
Now, imagine I want to swap the container "Left" (I want to move the Div, not the button) to the second row (LeftAndRightRwo2).
I tried various things but not of them worked... My last attempt:
$(document).ready(function () {
$("#SwapButton").click(function () {
alert('trying to swap');
$("#Left").remove();
$("#LeftAndRightRow2").append($("#Left"));
});
});
etc.
None of what I tried worked.
Can someone help understand how to move div from one place to another.
Thank you
When you remove an element, it is no longer on the DOM. If you want to move a DOM element like that you need to store it in a variable. Also you should use detach instead of remove as it is more efficient in this case.
$(document).ready(function () {
$("#SwapButton").click(function () {
var left = $('#Left');
left.detach();
$("#LeftAndRightRow2").append(left);
});
});
When you use $("#Left").remove() you are actually removing it from the DOM, so you can't recall it later with $("#LeftAndRightRow2").append($("#Left"));.
Try to save it, like this, in a variable:
JS
$("#SwapButton").click(function () {
alert('trying to swap');
var left = $("#Left");
$(left).remove();
$("#LeftAndRightRow2").append($(left));
});
Now the entire div moves from the right to the left.
fiddle
http://jsfiddle.net/mgR5b/26/
I think your problem is more of CSS and less in JS I have changed your mark up and CSS to get the result you are looking for
Markup
<div id="LeftAndRightRow1" class="rows">
<div id="Left">
<button id="B1">B1</button>
</div>
<div id="Right">
<button id="B2">B2</button>
</div>
</div>
<div id="LeftAndRightRow2" class="rows">
<div id="Left2">
<button id="B3">B3</button>
</div>
<div id="Right2">
<button id="B4">B4</button>
</div>
</div>
<br style="clear:both;" />
<button id="SwapButton">Swap Button</button>
JS
$(document).ready(function () {
$("#SwapButton").click(function () {
//alert('trying to swap');
// $("#Left").remove();
$("#LeftAndRightRow2").append($("#Left"));
});
});
CSS
.rows{
display:block;
clear:left;
}
.rows div{
float:left;
}
JSFIDDLE

HTML/Javascript spoiler script not working in IE (Show/Hide)

<div id="z1" onclick="document.getElementById('q1').style.display=''; document.getElementById('z1').style.display='none';" style="border:solid 1px; background- color: #DDDDDD; width:936px; height:auto; margin-left:0px;margin-right:0px;">
<h2> Pg. 419-423, problems 8-14 even, 20-30 odd, AYP 1-10</h2></div>
<div id="q1" onclick="document.getElementById('q1').style.display='none'; document.getElementById('z1').style.display='';" style="display:none; border:solid 1px; background-color: #DDDDDD; width:938px; height:auto;margin-left:0px;margin-right:0px;">
<h2> Pg. 419-423, problems 8-14 even, 20-30 odd, AYP 1-10</h2>
</br>
<img src="http://www.rediker.com/reports/samples/Attendance-Period/Homework-Assignment- form.jpg"></img></div>
The above script is what I planned on using to create a spoiler. Basically, when the user clicks on the z1 element, it hides itself and shows the q1 element, and vice-versa. It works on all browsers besides IE. Linking Jquery would be a small liability.
I think this is what you're looking for. You're missing to add display: block
Change this
document.getElementById('z1').style.display='';
to
document.getElementById('z1').style.display='block';
You should think about cleaning your code.
HTML:
<div id="z1" onclick="aa()"></div>
<div id="q1" onclick="bb()">
<img src="http://www.rediker.com/reports/samples/Attendance-Period/Homework-Assignment-form.jpg"/>
</div>
JS:
function aa() {
document.getElementById('q1').style.display = 'block';
document.getElementById('z1').style.display = 'none';
}
function bb() {
document.getElementById('q1').style.display = 'none';
document.getElementById('z1').style.display = 'block';
}
Working JSfiddle

how to determine the element ID of div that is positioned x pixels to the right of visible screen?

Hitting a wall with this one, hope someone can lend a hand. I have a wrapper div containing many fixed-width "content" divs. It's like a table, except that the number of items "per row" aren't fixed, so that whenever the screen size is wide, more items fit onto the screen. Pretty basic.
Also, each of these "content" divs has an adjacent "details" div that is hidden by default ("style=display:none"), and an adjacent "separator" div that is empty, containing only the style "clear:both;".
Each content/details/separator div has a unique number in its ID, so that I can tell they are related (e.g., content123, details1234, separator1234)
Now, when one of these content divs is clicked, I want to reveal its "details" div below it. That part, I've got working partially, by wrapping an anchor tag around the content div, which fires an onClick javascript event, which in turns runs a jQuery statement to make visible the details and separator divs jQuery(".details1234").css("display","block");"
But you can imagine my problem. Once that "separator" div is reveled, it pushes down (clears) any "content" divs that appears to the right of it, ugly. My thought, what I have been wrestling with for hours, is to reveal the "separator" div of the content div, that is the last one appearing in the "row" that was clicked. That way, a new "row" will be opened up by the separator, so that when the "content" div is revealed it appears below the clicked item in the new row. To do that, I need to figure out the elementID of the last content div in the "row", and I was thinking about using the Y-coord of the mouse click event, plus the X-coord = to the right-most edge of the wrapper div minus half the width of the fixed-width content div. Something like that. But I am smashed into a wall and can't figure it out.
Can anyone help me do that? Or offer a different solution?
If sample code would help let me know, I could whip up an example, but it may take some screen space in this post.
Thanks everyone.. going nuts with this.
EDIT: the sample code below is based on my site. When a cell is clicked, you can see its "details" div appear below it, but unfortunately the other divs in the "row" get pushed down. that is the effect I'm trying to avoid. When a cell is clicked, I want the "details" to appear below it, but also the other divs to stay in their positions above the other cell's details, basically I want to keep the "row" intact. In the code, you can see my fruitless experiments using a "separator" div, because my assumption is that if I can insert that after the last div in the row, then the "details" div will become the next row, followed then by the next row of cells. Hope I explained it OK. Thanksgiving feast causing blood to divert from brain ;)
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style type="text/css">
#overallwrapper{
background: #CCCCCC;
padding-top: 4px;
padding-left: 4px;
padding-right: 4px;
padding-bottom: 4px;
margin-top: 5px;
}
.contentcell{
border: 2px solid blue;
padding: 4px;
float: left;
width: 200px;
}
.separator{
clear:both;
display: none;
}
.details{
background:lightgreen;
border: 2px solid green;
width:450px;
display:none;
float:left;
clear:both;
}
</style>
<script type="text/javascript">
function showDetails(contentid){
//first, reset all highlights and close any open content divs
$("#overallwrapper .contentcell").css("border","2px solid blue");
$(".details").css("display","none");
$(".separator").css("display","none");
//now highlight the clicked div and reveal its content div
var contentHI = "#content"+contentid;
var detailsON = "#details"+contentid;
var separatorON = "#separator"+contentid;
$(contentHI).css("border","2px solid green");
//$(separatorON).css("display","block");
$(detailsON).css("display","block");
}
</script>
</head>
<body>
<div id="overallwrapper">
<div id="contentwrapper01">
<div id="content01" class="contentcell">cell01</div>
<div id="details01" class="details">here are details about cell01</div>
<div id="separator01" class="separator"> </div>
</div>
<div id="contentwrapper02">
<div id="content02" class="contentcell">cell02</div>
<div id="details02" class="details">here are details about cell02</div>
<div id="separator02" class="separator"></div>
</div>
<div id="contentwrapper03">
<div id="content03" class="contentcell">cell03</div>
<div id="details03" class="details">here are details about cell03</div>
<div id="separator03" class="separator"></div>
</div>
<div id="contentwrapper04">
<div id="content04" class="contentcell">cell04</div>
<div id="details04" class="details">here are details about cell04</div>
<div id="separator04" class="separator"></div>
</div>
<div id="contentwrapper05">
<div id="content05" class="contentcell">cell05</div>
<div id="details05" class="details">here are details about cell05</div>
<div id="separator05" class="separator"></div>
</div>
<div id="contentwrapper06">
<div id="content06" class="contentcell">cell06</div>
<div id="details06" class="details">here are details about cell06</div>
<div id="separator06" class="separator"></div>
</div>
<div style="clear:both;"></div><!-- to prevent parent collapse -->
</div>
</body>
</html>
User ,
if you give as regular position be default , it pushes the other contents definetly down as they come in squence.
Change the hidden divs position to absolute so that it will go out of sequence and you can position at anywhere on the page by top and left property.
get the offset of the div you want next to...
http://api.jquery.com/offset/
it will have top and left property , use those property's and position next to them.
let me know if you need anything else.
give a bigger z-index for the hidden divs.
What about showing the details div with position: absolute, on top of everything else? (See here, the code's a little messy but you get the idea).
I partially figured it out, but the logic may be very clunky. I basically walk left by 100px from the width of the container div until I find a content div. Plus it doesn't work in IE8, because IE is not getting the same results from jQuery's offset() or position() as firefox, it always reports "19". So in IE, I can never get a Y-coordinate value. I'm too sleepy now to work on this anymore today. If someone can lend a hand or tell me how to improve the javascript that would be cool.
Here is the working code for Firefox (I changed javascript and css of the detail divs, compared to original question):
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style type="text/css">
#overallwrapper{
background: #CCCCCC;
padding-top: 4px;
padding-left: 4px;
padding-right: 4px;
padding-bottom: 4px;
margin-top: 5px;
}
.contentcell{
border: 2px solid blue;
padding: 4px;
float: left;
width: 200px;
}
.separator{
clear:both;
display: none;
}
.details{
background:lightgreen;
border: 2px solid green;
display:none;
clear:both;
}
</style>
<script type="text/javascript">
function showDetails(contentid){
//first, reset all highlights and close any open content divs
$("#overallwrapper .contentcell").css("border","2px solid blue");
$(".details").css("display","none");
$(".separator").css("display","none");
//now highlight the clicked div and reveal its content div
//first, figure out which separator to display.
//1.get the y-pos from the clicked element, this gives y-coord of the row
contentClicked = "#content"+contentid;
var clickedoffset = $(contentClicked).offset();
var ypos = clickedoffset.top;
var wrapperwidth = $("#overallwrapper").width();
for (var xpos=wrapperwidth; xpos>0; xpos-=100){
var elematpos = document.elementFromPoint(xpos, ypos);
var elematposid = elematpos.id;
if (elematposid.substring(0,7) == "content") {
var lastcontentdivID = elematposid.substring(7);
break;
}
}
$(contentClicked).css("border","2px solid green");
var detailsON = "#details"+contentid;
$(detailsON).css("display","block");
var lastidonscreen = "#content"+lastcontentdivID;
$(detailsON).insertAfter(lastidonscreen);
}
</script>
</head>
<body>
<div id="overallwrapper">
<div id="contentwrapper01">
<div id="content01" class="contentcell">cell01</div>
<div id="separator01" class="separator"> </div>
<div id="details01" class="details">here are details about cell01</div>
</div>
<div id="contentwrapper02">
<div id="content02" class="contentcell">cell02</div>
<div id="separator02" class="separator"></div>
<div id="details02" class="details">here are details about cell02</div>
</div>
<div id="contentwrapper03">
<div id="content03" class="contentcell">cell03</div>
<div id="separator03" class="separator"></div>
<div id="details03" class="details">here are details about cell03</div>
</div>
<div id="contentwrapper04">
<div id="content04" class="contentcell">cell04</div>
<div id="separator04" class="separator"></div>
<div id="details04" class="details">here are details about cell04</div>
</div>
<div id="contentwrapper05">
<div id="content05" class="contentcell">cell05</div>
<div id="separator05" class="separator"></div>
<div id="details05" class="details">here are details about cell05</div>
</div>
<div id="contentwrapper06">
<div id="content06" class="contentcell">cell06</div>
<div id="separator06" class="separator"></div>
<div id="details06" class="details">here are details about cell06</div>
</div>
<div style="clear:both;"></div><!-- to prevent parent collapse -->
</div>
</body>
</html>
EDIT:
Blasted IE. I just can't trust it to determine screen coordinates. I got it working though, but only for Firefox. again IE is trying to drive me insane by not handling insertAfter properly. arrgh! here is the final code:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style type="text/css">
#overallwrapper{
background: #CCCCCC;
padding-top: 4px;
padding-left: 4px;
padding-right: 4px;
padding-bottom: 4px;
margin-top: 5px;
}
.contentwrapper{
}
.contentcell{
padding: 4px;
float: left;
width: 200px;
height: 100px;
border: 2px solid blue;
}
.separator{
clear:both;
display: none;
}
.details{
background:lightgreen;
border: 2px solid green;
display:none;
clear:both;
}
</style>
<script type="text/javascript">
function showDetails(contentid){
//first, reset all highlights and close any open content divs
$("#overallwrapper .contentcell").css("border","2px solid blue");
$(".details").css("display","none");
$(".separator").css("display","none");
var contentClicked = "#content"+contentid;
var thisypos = $(contentClicked).offset().top;
var nextdivid = contentClicked;
var countid = contentid;
do
{
var prevdivid = nextdivid;
var nextcontentid = (countid * 1) + 1;
var nextcontentid = '' + nextcontentid;
if ( nextcontentid.length < 2)
{ nextcontentid = "0" + nextcontentid; }
nextdivid = "#content" + nextcontentid;
if ( $(nextdivid).length ) {
var nextypos = $(nextdivid).offset().top;
countid++;
} else {
break;
}
}
while (thisypos == nextypos);
$(contentClicked).css("border","2px solid green");
var detailsON = "#details"+contentid;
$(detailsON).css("display","block");
$(detailsON).insertAfter(prevdivid);
}
</script>
</head>
<body>
<div id="overallwrapper">
<div id="contentwrapper01" class="contentwrapper">
<div id="content01" class="contentcell">cell01</div>
<div id="separator01" class="separator"> </div>
<div id="details01" class="details">here are details about cell01</div>
</div>
<div id="contentwrapper02" class="contentwrapper">
<div id="content02" class="contentcell">cell02</div>
<div id="separator02" class="separator"></div>
<div id="details02" class="details">here are details about cell02</div>
</div>
<div id="contentwrapper03" class="contentwrapper">
<div id="content03" class="contentcell">cell03</div>
<div id="separator03" class="separator"></div>
<div id="details03" class="details">here are details about cell03</div>
</div>
<div id="contentwrapper04" class="contentwrapper">
<div id="content04" class="contentcell">cell04</div>
<div id="separator04" class="separator"></div>
<div id="details04" class="details">here are details about cell04</div>
</div>
<div id="contentwrapper05" class="contentwrapper">
<div id="content05" class="contentcell">cell05</div>
<div id="separator05" class="separator"></div>
<div id="details05" class="details">here are details about cell05</div>
</div>
<div id="contentwrapper06" class="contentwrapper">
<div id="content06" class="contentcell">cell06</div>
<div id="separator06" class="separator"></div>
<div id="details06" class="details">here are details about cell06</div>
</div>
<div style="clear:both;"></div><!-- to prevent parent collapse -->
</div>
</body>
</html>

Categories

Resources