How it should work:
Open (toggle .show class) on div.user, and displays the .userSub div.
If I click on another div.user, close (remove .show class) and opens the clicked div.userSub
If I click on the already .show-ed div.user (NOT .userSub), it'd close the target div.user.
Almost works but the problem:
when .userSub div is .show-ed, I can only click to close on the .userSub div, not the .user div. However that would be goal. :)
I've tried to eliminate the problem. Probably the .user selection is wrong and I should use stopPropagation() somewhere, or I should be more specific with the child elements, but I can't figure it out.
let $active
$(document).ready(() => {
$(".user").click(function(e) {
if ($active != null) {
$active.toggleClass("show")
}
$(e.target).children().toggleClass("show")
$active = $(e.target).children()
})
})
.user {
background-color: gray;
padding: 20px;
margin: 5px;
}
.userSub {
display: none;
padding: 20px;
background-color: lightgray;
color: black;
margin: 5px;
}
.show {
display: block;
}
button {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="user">
name 1
<div class="userSub">details 1<button>more 1</button></div>
</div>
<div class="user">
name 2
<div class="userSub">details 2<button>more 2</button></div>
</div>
<div class="user">
name 3
<div class="userSub">details 3<button>more 3</button></div>
</div>
To achieve your goal check that the clicked element was the .user element directly, not a child of it. To do that you can use the target property of the event.
Also note that you can simplify the logic by only applying the .show class to the parent .user and having the CSS rules apply the display: block rule to the child elements based on the class on a parent. Try this:
$(document).ready(() => {
let $users = $(".user").click(function(e) {
if (e.target !== this)
return;
$users.not(this).removeClass('show');
$(this).toggleClass("show")
})
})
.user {
background-color: gray;
padding: 20px;
margin: 5px;
}
.userSub {
display: none;
padding: 20px;
background-color: lightgray;
color: black;
margin: 5px;
}
.user.show .userSub {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="user">
name 1
<div class="userSub">details 1<button>more 1</button></div>
</div>
<div class="user">
name 2
<div class="userSub">details 2<button>more 2</button></div>
</div>
<div class="user">
name 3
<div class="userSub">details 3<button>more 3</button></div>
</div>
Related
I'm working on an app where I am using the DOMTokenList for shown and hidden an element using add() and remove() methods but the code is getting big. Example:
button.addEventListener("click", () => {
elem1.classList.add("display-none");
elem2.classList.remove("display-none");
elem3.classList.remove("display-none");
elem4.classList.add("display-none");
});
button2.addEventListener("click", () => {
elem1.classList.remove("display-none");
elem2.classList.add("display-none");
elem3.classList.add("display-none");
elem4.classList.remove("display-none");
});
I've seven code parts similar to the example and I started looking others ways but I tried with CSS
.box {
--displayRed: block;
--displayGreen: none;
--displayBlue: none;
height: 100px;
width: 100px;
}
.box--red {
display: var(--displayRed);
background-color: #ff0000;
}
.box--green {
display: var(--displayGreen);
background-color: #00ff00;
}
.box--blue {
display: var(--displayBlue);
background-color: #0000ff;
}
.box--red:target {
--displayRed: block;
--displayGreen: none;
--displayBlue: none;
}
.box--green:target {
--displayRed: none;
--displayGreen: block;
--displayBlue: none;
}
.box--blue:target {
--displayRed: none;
--displayGreen: none;
--displayBlue: block;
}
<div>
<div class="box">
<div id="boxRed" class="box box--red"></div>
<div id="boxGreen" class="box box--green"></div>
<div id="boxBlue" class="box box--blue"></div>
</div>
<nav>
<ul>
<li>Show red box</li>
<li>Show green box</li>
<li>Show blue box</li>
</ul>
</nav>
</div>
I was expecting the red box to hide when I clicked the link to show the yellow box but it only scrolled down and didn't hide the red box. I tried to find others solution with videos, Pseudo-classes, Selectors, even with two question that are Can text be hidden and shown using just CSS (no JavaScript code)? [closed] and Ocultar y Mostrar un elemento css but I can't find the perfect idea. I ACCEPT SUGGESTIONS
Since your goal is to hide the elements with display:none, I'll assume the DOM order doesn't matter.
So what you can do with no JS is to place your default element at the end of the container and then hide it when it's following a :targeted element:
.box {
--displayRed: block;
--displayGreen: none;
--displayBlue: none;
height: 100px;
width: 100px;
}
.box--red {
display: var(--displayRed);
background-color: #ff0000;
}
.box--green {
display: var(--displayGreen);
background-color: #00ff00;
}
.box--blue {
display: var(--displayBlue);
background-color: #0000ff;
}
:target ~ .box--red { /* when any other than red is selected */
--displayRed: none;
}
.box--green:target {
--displayRed: none;
--displayGreen: block;
--displayBlue: none;
}
.box--blue:target {
--displayRed: none;
--displayGreen: none;
--displayBlue: block;
}
<div>
<div class="box">
<div id="boxGreen" class="box box--green"></div>
<div id="boxBlue" class="box box--blue"></div>
<!-- move the default one last -->
<div id="boxRed" class="box box--red"></div>
</div>
<nav>
<ul>
<li>Show red box</li>
<li>Show green box</li>
<li>Show blue box</li>
</ul>
</nav>
</div>
Ps: as noted by T.J Crowder in the comments below, the css-variables in here are quite useless as they won't actually reach the elements they're supposed to affect.
So you could rewrite all this without these variables and simply do:
.box {
height: 100px;
width: 100px;
}
/* might be better to remove the "box" class on the container but... */
.box[id] {
display: none;
}
.box:target ~ .box--red {
display: none;
}
.box:target {
display: block;
}
/* This one is the default one */
.box.box--red {
background-color: #ff0000;
display: block;
}
/* only the color needs to be defined for the others */
.box--green {
background-color: #00ff00;
}
.box--blue {
background-color: #0000ff;
}
<div>
<div class="box">
<div id="boxGreen" class="box box--green"></div>
<div id="boxBlue" class="box box--blue"></div>
<!-- move the default one last -->
<div id="boxRed" class="box box--red"></div>
</div>
<nav>
<ul>
<li>Show red box</li>
<li>Show green box</li>
<li>Show blue box</li>
</ul>
</nav>
</div>
The trick is defaulting to the red box when there's no hash fragment. I'm not sure you can do that with a pure CSS solution. (Edit: it turns out you can, as shown by Kaiido, provided you can put the default at the end; I should have thought of the general sibling combinator! But the CSS custom properties [aka "variables"] aren't actually doing anything in that AFAICS, see this fiddle.)
I don't know if it's possible to do this with CSS custom properties (aka "variables"); I tend to think not, since setting a custom property in the selector rule for one sibling (say, .box--blue:target) doesn't change its value for any elements not inside that .box--blue element (doesn't change it for the .box--red sibling, for instance). (Fiddle.) But you can definitely show/hide boxes with the old checkbox/radio button trick:
Define the boxes as display: none.
Have invisible radio buttons immediately prior to the box the radio button will relate to in the same parent:
<input type="radio" name="box-controller" id="chk-box--red" checked>
<div id="boxRed" class="box box--red"></div>
Have label elements that tick the radio button for that box (via id/for) instead of links.
<label tab-index="0" for="chk-box--red">Show red box</label>
In the above, note that I've added tab-index to the label so it show sup in the tabbing order. We probably also want CSS that underlines it or similar.
Have a CSS rule that says the .box immediately after a checked radio button should be display: block:
input[name=box-controller]:checked + .box {
display: block;
}
Live example:
.box {
height: 100px;
width: 100px;
}
/* Hide boxes inside the outer box until/unless shown */
.box .box {
display: none;
}
label {
text-decoration: underline;
}
input[name=box-controller] {
display: none;
}
input[name=box-controller]:checked + .box {
display: block;
}
.box--red {
background-color: #ff0000;
}
.box--green {
background-color: #00ff00;
}
.box--blue {
background-color: #0000ff;
}
<div>
<div class="box">
<input type="radio" name="box-controller" id="chk-box--red" checked>
<div id="boxRed" class="box box--red"></div>
<input type="radio" name="box-controller" id="chk-box--green">
<div id="boxGreen" class="box box--green"></div>
<input type="radio" name="box-controller" id="chk-box--blue">
<div id="boxBlue" class="box box--blue"></div>
</div>
<nav>
<ul>
<li><label tab-index"0" for="chk-box--red">Show red box</label></li>
<li><label tab-index"0" for="chk-box--green">Show green box</label></li>
<li><label tab-index"0" for="chk-box--blue">Show blue box</label></li>
</ul>
</nav>
</div>
Important caveat: With the above, the URL is no longer driving the process (it doesn't change as you choose boxes). That's because you can't have a label inside an a, and if you put the a inside the label because it prevents the label doing its job. If having the state in the URL is important to you, look at the JavaScript solution (which doesn't repeat itself) below.
You indicated that your concern with the JavaScript solution was that you were repeating a lot of code. There's no need to do that. Just for what it's worth, here's a JavaScript solution driven the hash fragment that works regardless of how many boxes you have; comments within:
// The function that shows the current box, hiding others
function showCurrentBox() {
// Get the box to show, defaulting to `boxRed`
const hash = location.hash.replace(/^#/, "") || "boxRed";
// Hide any box we've previously shown
// (Note the use of optional chaining, since `querySelector` may return `null`)
document.querySelector(".box.showing")?.classList.remove("showing");
// Show the box (again with optional chaining in case the fragment doesn't identify a box)
document.getElementById(hash)?.classList.add("showing");
}
// Run on startup
showCurrentBox();
// Run whenver the hash changes
window.addEventListener("hashchange", showCurrentBox);
.box {
height: 100px;
width: 100px;
}
/* Hide boxes inside the outer box until/unless shown */
.box .box {
display: none;
}
.box.showing {
display: block;
}
.box--red {
background-color: #ff0000;
}
.box--green {
background-color: #00ff00;
}
.box--blue {
background-color: #0000ff;
}
<div>
<div class="box">
<div id="boxRed" class="box box--red showing"></div>
<div id="boxGreen" class="box box--green"></div>
<div id="boxBlue" class="box box--blue"></div>
</div>
<nav>
<ul>
<li>Show red box</li>
<li>Show green box</li>
<li>Show blue box</li>
</ul>
</nav>
</div>
Adding boxes to that is just a matter of defining their CSS and giving them an ID; the JavaScript code doesn't change.
With this solution, the URL does drive the boxes. Bookmark the URL showing the green box, and that's what shows when you come back to it.
You can keep things DRY with JavaScript by caching all the boxes first, and using data attributes instead of ids. Initialise all the boxes as display: none.
Add one event listener to the ul element (using event delegation to watch for events from its child elements as they "bubble up" the DOM), switch off all the boxes by looping over the boxes and removing a show class, and then adding a show class (display: block;) to the element that corresponds to the data-id of the button that was clicked.
You can add as many boxes/buttons as you want as you can see in this example.
// Cache the boxes, and the list element, and attach
// one listener to the list element
const boxes = document.querySelectorAll('.box');
const list = document.querySelector('ul');
list.addEventListener('click', handleClick);
function handleClick(e) {
// If the child element that fired the event
// is a button
if (e.target.matches('button')) {
// Destructure the id from its dataset
const { id } = e.target.dataset;
// Iterate over all the boxes removing the show class
boxes.forEach(box => box.classList.remove('show'));
// Grab the box which has a class that corresponds
// to the id: `.red` for example
const current = document.querySelector(`.${id}`);
// And then show that box
current.classList.add('show');
}
}
.box { display: none; padding: 0.3em; color: white; }
.show { display: block; }
.red { background-color: red; }
.green { background-color: green; }
.blue { background-color: blue; }
.orange { background-color: orange; }
.black { background-color: black; }
ul { margin-left: 0; padding: 0; }
li { display: inline; }
button:hover { cursor: pointer; background-color: #fffff0; }
<div>
<nav>
<ul>
<li><button data-id="red">Red</button></li>
<li><button data-id="green">Green</button></li>
<li><button data-id="blue">Blue</button></li>
<li><button data-id="orange">Orange</button></li>
<li><button data-id="black">Black</button></li>
</ul>
</nav>
<div class="boxes">
<div class="box red">Red</div>
<div class="box green">Green</div>
<div class="box blue">Blue</div>
<div class="box orange">Orange</div>
<div class="box black">Black</div>
</div>
</div>
Additional documentation
dataset
Destructuring assignment
matches
Currently when I click the 1st button both hidden divs are showing. My goal is when I click the 1st button it only shows the hidden div on its group. And if I click the 2nd button it only shows the hidden div on its group.
Can anyone help me with how to achieve it?
$(document).ready(function () {
$(".btn").on("click", function () {
$(".hidden").addClass("active");
});
$(".closed").on("click", function (){
$(".hidden").removeClass("active")
})
})
.btn {
background: blue;
color: #ffffff;
font-weight: 600;
padding: 20px;
}
.hidden {
width: 100px;
height: 100px;
background: yellow;
display: none;
}
.hidden.active {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="con">
<div class="btn">Show</div>
<div class="hidden">
<div class="closed">Click to hide</div>
</div>
</div>
</div>
<br>
<div class="container">
<div class="con">
<div class="btn">Show</div>
<div class="hidden">
<div class="closed">Click to hide</div>
</div>
</div>
</div>
Your code
$(".hidden").addClass("active");
selects all .hidden elements (hence your issue).
Within the click event you need to use this to refer to the element being clicked and then use relative DOM navigation to find the element you do want.
In your case it's the next sibling, so .next() will suffice for show, but hide/close will need to find its parent .hidden
$(document).ready(function () {
$(".show").on("click", function () {
$(this).next().addClass("active");
});
$(".close").on("click", function (){
$(this).closest(".hidden").removeClass("active")
})
})
.show {
background: blue;
color: #ffffff;
font-weight: 600;
padding: 20px;
}
.hidden {
width: 100px;
height: 100px;
background: yellow;
display: none;
}
.hidden.active {
display: block;
}
.close { background-color:#ccc; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="con">
<div class="show">Show</div>
<div class="hidden">
<div class="close">Click to hide</div>
</div>
</div>
</div>
<br>
<div class="container">
<div class="con">
<div class="show">Show</div>
<div class="hidden">
<div class="close">Click to hide</div>
</div>
</div>
</div>
I have 2 pricing plan boxes with toggles for monthly and annual pricing. When i click Pay monthly on the Core box, all the numbers change correctly, but what i would like to do is to click either core or pro, and then both move. At the moment, only the Core toggle moves. See the code below, and link to the demo on my staging site.
jQuery
$(document).ready(function() {
var checkBoxes = $("input[name='toggle']");
toggle();
$("#toggle").click(function() {
toggle();
});
function toggle() {
if (checkBoxes.prop("checked")) {
$('#coreMonthlyText,#coreMonthlyPrice,#proMonthlyText,#proMonthlyPrice').show();
$('#coreAnnuallyText,#coreAnnuallyPrice,#proAnnuallyText,#proAnnuallyPrice').hide();
} else {
$('#coreMonthlyText,#coreMonthlyPrice,#proMonthlyText,#proMonthlyPrice').hide();
$('#coreAnnuallyText,#coreAnnuallyPrice,#proAnnuallyText,#proAnnuallyPrice').show();
}
}
});
HTML
<div class="pricing-box" id="core-box">
<div class="row">
<div class="column">
<div class="core">
<h2>Core Plan</h2>
</div>
</div>
<div class="column">
<div id="coreAnnuallyPrice" class="coreAnnuallyPrice">
<h2>$2,399/yr</h2> Normally $3,588/yr
</div>
<div id="coreMonthlyPrice" class="coreMonthlyPrice">
<h2>$99/pm</h2> first 2 months free
</div>
</div>
</div><div class="center_text">
<label for="toggle" class="toggle-switch"><input class="toggle-button" id="toggle" type="checkbox" name="toggle" data-checked="coreAnnuallyPrice,coreAnnuallyText" data-not-checked="coreMonthlyPrice,coreMonthlyText"><span>Pay annually</span><span>Pay monthly</span>
</label>
</div></div>
<div class="pricing-box" id="pro-box">
<div class="row">
<div class="column">
<div class="pro">
<h2>Pro Plan</h2>
</div>
</div>
<div class="column">
<div id="proAnnuallyPrice" class="proAnnuallyPrice">
<h2>$4,199/yr</h2> Normally $5,988/yr
</div>
<div id="proMonthlyPrice" class="proMonthlyPrice">
<h2>$299/pm</h2> first 2 months free
</div>
</div>
</div><div class="center_text">
<label for="toggle" class="toggle-switch"><input class="toggle-button" id="toggle" type="checkbox" name="toggle" data-checked="proAnnuallyPrice,proAnnuallyText" data-not-checked="proMonthlyPrice,proMonthlyText"><span>Pay annually</span>.
<span>Pay monthly</span>
</label>
</div>
</div>
CSS
.toggle-switch {
cursor: pointer;
background-color: #F8F8F9;
display: inline-block;
border: 0;
padding-left: 0;
padding-right: 0;
}
.toggle-switch input {
display: none;
}
.toggle-switch,
.toggle-switch span {
border-radius: 35px;
border-style: solid;
border-color: transparent;
padding-top: 20px;
padding-bottom: 20px;
}
.toggle-switch span {
border-width: 2px;
padding-left: 20px;
padding-right: 20px;
}
.toggle-switch input:checked+span+span,
.toggle-switch input+span {
border: 1px solid #00AEEF !important;
background-color: white !important;
}
.toggle-switch input+span+span,
.toggle-switch input:checked+span {
background-color: #F8F8F9 !important;
border-color: transparent !important;
}
#coreMonthlyText,
#coreMonthlyPrice,
#coreAnnuallyText,
#coreAnnuallyPrice {
display: none;
}
#proMonthlyText,
#proMonthlyPrice,
#proAnnuallyText,
#proAnnuallyPrice {
display: none;
}
Link to webpage
You have duplicate IDs, which will cause problems. You can have duplicate name="toggle" but you likely need to distinguish them somehow.
The main problem is that the state of the toggle changes automatically and you never set the state of the other. It is important to know which one was clicked.
Hence, to keep them "in sync", you can set both toggles to the state of the one that was clicked:
$(document).ready(function() {
var checkBoxes = $("input[name='toggle']");
toggle(checkBoxes.el(0)); // pass first checkbox for initial state
checkBoxes.click(function(event) {
toggle(event.target);
});
function toggle(target) {
var checked = $(target).prop('checked')
// set all checkboxes to the same state
checkBoxes.prop('checked', checked)
// set other properties based on checked state
$('#coreMonthlyText,#coreMonthlyPrice,#proMonthlyText,#proMonthlyPrice')
.toggle(checked)
$('#coreAnnuallyText,#coreAnnuallyPrice,#proAnnuallyText,#proAnnuallyPrice')
.toggle(!checked)
}
});
I have a header with 3 links, all linking to a specific div with a corresponding id:
body {
font-size: 32px;
}
.links {
display: flex;
a {
padding: 10px;
}
}
.box:not(:target) {
display: none;
}
#box1 {
background-color: crimson;
}
#box2 {
background-color: darkgreen;
}
#box3 {
background-color: gold;
}
<div class="links">
Box1
Box2
Box3
</div>
<div class="box" id="box1">Box1 content</div>
<div class="box" id="box2">Box2 content</div>
<div class="box" id="box3">Box3 content</div>
I want to use the CSS pseudo class to turn the selected element from display:none to display:block when targetted. I achieved this using .box:not(:target) { display:none }.
The problem is that I would like to default the boxes to show the first box (#box1) if :target does not exist amongst the three boxes with css only if possible, any help would be greatly appreciated!
Follow below the snippet, hope your problem will fix with html and css,
body {
font-size: 32px;
}
.links {
display: flex;
a {
padding: 10px;
}
}
.box:not(:target) {
display: none;
}
#box1{
display: block;
}
#box2:target ~ #box1,
#box3:target ~ #box1{
display: none;
}
#box1 {
background-color: crimson;
}
#box2 {
background-color: darkgreen;
}
#box3 {
background-color: gold;
}
<div class="links">
Box1
Box2
Box3
</div>
<div class="box" id="box2">Box2 content</div>
<div class="box" id="box3">Box3 content</div>
<div class="box" id="box1">Box1 content</div>
the box1 is default and when you trigger the box2, box3 you can see box1 will get display none.
mainly it's working for "general sibling selector (~)"
Create new css class called BoxControl consider this in all <a> elements for trigger show/hide event.
Then Add .active css class and add it to default class list of the element you want to show in first view as shown bellow.
Finally, implement JS as for your HTML structure tell that is your best option otherwise you'll have to modify your HTML.
SCSS/JS
Codepen Link
CSS/JS
// Capture all elements of <a> output should be in an array
var box = document.getElementsByClassName("boxControl");
// Create new variable i stands for index to be used in for loop
var i;
// Loop through all elements been found in box variable
for (i = 0; i < box.length; i++) {
// Add Event Listener of Click for <a> elements found in the variable box
box[i].addEventListener("click", function(e) {
// Add e in callback function trace the element triggered this event
// In other words, which button has been clicked
var clicked =
e.target.getAttribute("href");
// Tricky bit: As per your request they should be at least
// one default active element otherwise or else will not remove any active class
if (document.querySelector(".active")) {
document.querySelector(".active").classList.remove("active");
// Use value of captured attribute (href) to target and toggle the new active class
document.querySelector(clicked).classList.toggle("active");
// Not found any active css class (box)
} else {
var clicked = e.target.getAttribute("href");
document.querySelector(clicked).classList.toggle("active");
}
})
};
body {
font-size:32px;
}
.links {
display:flex;
}
.links a {
padding:10px;
}
.active {
display:block!important;
}
#box1 {
background-color:crimson;
display: none;
}
#box2 {
background-color:darkgreen;
display: none;
}
#box3 {
background-color:gold;
display:none;
}
<div class="links">
Box1
Box2
Box3
</div>
<div class="box active" id="box1">Box1 content</div>
<div class="box" id="box2">Box2 content</div>
<div class="box" id="box3">Box3 content</div>
Why "zawartosc prawo" do not hide "zawartosc lewo"? on hover effect?
I try change class name, add ID, and another, but still do not work :(
Maybe I must try add some JS? But I think someone know how to do this.
Code
.zamieniamlewo {
display: none;
}
.prawo:hover ~ .zamieniamlewo {
display: block !important;
}
.lewo:hover ~ .prawo {
display: none;
}
.zamieniamprawo {
display: none;
}
.lewo:hover ~ .zamieniamprawo {
display: block !important;
}
.lewo {
background: red;
padding: 20px;
}
.prawo {
background: green;
padding: 20px;
}
<div class="lewo">
<h3>Zawartość lewo</h3>
</div>
<div class="prawo">
<h3>Zawartość prawo</h3>
</div>
<div class="zamieniamlewo">
<h3 class="imie">zamieniamlewo</h3>
</div>
<div class="zamieniamprawo">
<h3>zamieniam prawo</h3>
</div>
The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.
for example:
.red ~ p{
color:red;
}
<p>i m not red</p>
<p class='red'>i m not red</p>
<p>i m red</p>
<p>i m red</p>
here the last two paragraphs match the sibling selector.
So in your code you can change something like
<div class="prawo">
<h3>Zawartość prawo</h3>
</div>
<div class="lewo">
<h3>Zawartość lewo</h3>
</div>
and add this in .css file
.prawo:hover ~ .lewo {
display:none;
}
I guess you need some thing like this , i have used jquery as it seems pretty straight forward to use when you usually deal with events
$(document).ready(function(){
$(".lewo").mouseenter(function(){
$(".prawo").hide();
});
$(".lewo").mouseleave(function(){
$(".prawo").show();
$(".lewo").show();
});
$(".prawo").mouseenter(function(){
$(".lewo").hide();
});
$(".prawo").mouseleave(function(){
$(".prawo").show();
$(".lewo").show();
});
});
.zamieniamlewo {
display: none;
}
.zamieniamprawo {
display: none;
}
.lewo:hover ~ .zamieniamprawo {
display: block !important;
background:white;
}
.prawo:hover ~ .zamieniamlewo {
display: block !important;
background: white;
}
.lewo {
background: red;
padding: 20px;
}
.prawo {
background: green;
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="lewo">
<h3>Zawartość lewo</h3>
</div>
<div class="prawo">
<h3>Zawartość prawo</h3>
</div>
<div class="zamieniamlewo">
<h3 class="imie">zamieniamlewo</h3>
</div>
<div class="zamieniamprawo">
<h3>zamieniam prawo</h3>
</div>