container hide and show - javascript

I want to hide a container when we click on remove button inside container and same time in navigation the name of container will be visible.
Now when we click on navigation link of that div, the container will be visible and hide the link from navigation.
I created one to make you more clear :
JSFIDDLE
HTML
<div id="1" class="main-container">
Remove
</div>
<div id="2" class="main-container">
Remove
</div>
<div id="3" class="main-container">
Remove
</div>
<div id="4" class="main-container">
Remove
</div>
<div class="navigation">
Container 1
Container 2
Container 3
Container 4
</div>
CSS
.main-container {
width:100px;
height:50px;
margin:10px;
float:left;
background-color:grey;
}
JS
$('.rem-widget').live("click", function() {
var currentId2 = $(this).parents(".main-container").attr('id');
$('#' + currentId2).hide('slow');
var currentId2 = $(this).parents(".nav-widget-add").attr('id');
$('#' + currentId2).show('slow');
});
$('.nav-widget-add').live("click", function() {
var currentId2 = $(this).parents(".main-containe").attr('id');
$('#' + currentId2).show('slow');
var currentId2 = $(this).parents("").attr('id');
$('#' + currentId2).hide('slow');
});

Try this,
HTML
<div id="1" class="main-container"> Remove
</div>
<div id="2" class="main-container"> Remove
</div>
<div id="3" class="main-container"> Remove
</div>
<div id="4" class="main-container"> Remove
</div>
<div class="navigation"> Container 1
Container 2
Container 3
Container 4
</div>
css
.main-container {
width:100px;
height:50px;
margin:10px;
float:left;
background-color:grey;
}
.nav-widget-add {
display:none;
}
this javascript code will make the container transparent when click on the remove link.
SEE THIS FIDDLE DEMO
$('.rem-widget').click(function () {
$(this).hide('slow');
$(this.parentNode).css('background-color', 'transparent');
$('a[data-navi=' + this.parentNode.id + ']').show('slow');
});
$('.nav-widget-add').click(function () {
var navIndex = $(this).index() + 1;
$('#' + this.dataset.navi).css('background-color', 'grey').children('.rem-widget').show('slow');
$(this).hide('slow');
});
this javascript code will hide the container when click on the remove link.
$('.rem-widget').click(function () {
$(this.parentNode).hide('slow');
$('a[data-navi=' + this.parentNode.id + ']').show('slow');
});
$('.nav-widget-add').click(function () {
$('#' + this.dataset.navi).show('slow');
$(this).hide('slow');
});
SEE THIS FIDDLE DEMO

i think this is what you are looking for
<script>
$( document ).ready(function() {
$('.main-container a.rem-widget').click(function(){
$(this).parent().hide('slow');
$('.'+$(this).attr('id')).show('slow');
});
$('.navigation a').click(function(){
$(this).hide('slow');
$('#'+$(this).attr('class')).parent().show('slow');
});
});
</script>
<body>
<div class="main-container">
Remove
</div>
<div class="main-container">
Remove
</div>
<div class="main-container">
Remove
</div>
<div class="main-container">
Remove
</div>
<div class="navigation">
Container 1
Container 2
Container 3
Container 4
</div>
</body>

Is this what you are looking for? Your question was quite confusing. But as per your comments, i think this is what you want. I hope I am write.
Change you HTML to this
<div id="1" class="main-container">
Remove
</div>
<div id="2" class="main-container">
Remove
</div>
<div id="3" class="main-container">
Remove
</div>
<div id="4" class="main-container">
Remove
</div>
<div class="navigation">
Container 1
Container 2
Container 3
Container 4
</div>
and your jQuery to this
$(document).ready(function() {
$('#container1').hide();
$('#container2').hide();
$('#container3').hide();
$('#container4').hide();
$('#maincontainer1').click(function() {
$('#1').hide('slow');
$('#container1').show();
});
$('#maincontainer2').click(function() {
$('#2').hide('slow');
$('#container2').show();
});
$('#maincontainer3').click(function() {
$('#3').hide('slow');
$('#container3').show();
});
$('#maincontainer4').click(function() {
$('#4').hide('slow');
$('#container4').show();
});
$('#container1').click(function() {
$('#1').show('slow');
$(this).hide();
});
$('#container2').click(function() {
$('#2').show('slow');
$(this).hide();
});
$('#container3').click(function() {
$('#3').show('slow');
$(this).hide();
});
$('#container4').click(function() {
$('#4').show('slow');
$(this).hide();
});
});
Here's a working JSFiddle
What I did was:
Added Id to each anchor tag (Remove link)
I have hidden all containers in navigation in the beginning
And the rest, you can see :)

Related

jQuery expandable input div

I have a form with inputs are tasks with headers and its description. Is there any effective way in jQuery to make the details under header of each input able to toggle every time the header is clicked?
Here is the HTML:
<div class="todo-task">
<div class="task-header">Sample Header</div>
<div class="task-date">25/06/2017</div>
<div class="task-description">Description</div>
</div>
You can do something like this:
$('.task-header').click(clickFunction)
function clickFunction () {
if ($('.task-description').is(":visible")) {
$('.task-description').hide()
} else {
$('.task-description').show()
}
}
Functioning example: https://codepen.io/rp3/pen/yLOGgKB
If you wanted a solution for multiple headers & descriptions, data attributes might be the way to go. Add data attributes to the html you posted:
<div class="todo-task" data-headergroup="1">
<div class="task-header" data-headergroup="1">Sample Header</div>
<div class="task-date" data-headergroup="1">25/06/2017</div>
<div class="task-description" style="display: none" data-headergroup="1">Description</div>
</div>
Then the js would be something more like this:
$('.task-header').click(clickFunction)
function clickFunction () {
let headerGroupNumber = this.getAttribute('data-headergroup')
if ($('.task-description').is(":visible")) {
$(".task-description[data-headergroup='" + headerGroupNumber +"']").hide()
} else {
$(".task-description[data-headergroup='" + headerGroupNumber +"']").show()
}
}
Example: https://codepen.io/rp3/pen/BaKvpOv
You can use the .toggle() function of jquery to show and hide details when the header is clicked.
Here I have wrapped your details with a new div with id="taskDetails" and when the header with id="taskHeader" is clicked it will toogle the details.
for further adding the toogle option you can refer https://api.jquery.com/toggle/
$("#taskHeader").click(function() {
$("#taskDetails").toggle();
});
.task-header {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="todo-task">
<div id="taskHeader" class="task-header">
Sample Header
</div>
<div id="taskDetails">
<div class="task-date">25/06/2017</div>
<div class="task-description">Description</div>
</div>
</div>
Updated:
If you have multiple divs with todo-task you can do by following:
$(function() {
$(document.body).on("click", "div.task-header", function() {
$(this).nextAll().toggle();
});
});
.task-header {
cursor: pointer;
}
.todo-task {
padding-bottom: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="todo-task">
<div class="task-header">Sample Header</div>
<div class="task-date">25/06/2017</div>
<div class="task-description">Description</div>
</div>
<div class="todo-task">
<div class="task-header">Sample Header</div>
<div class="task-date">25/06/2017</div>
<div class="task-description">Description</div>
</div>

remove element with specific class only when other class is active

I have an element that can be removed when it has the class "edit-mode". The class "edit-mode" is toggled by a button. The problem here is that when the "edit-mode" class gets removed, this element can be removed on click which shouldn't happen.
Consider the following code :
$(document).ready(function() {
$('.active-edit-mode').click(function() {
$('.my-element').toggleClass('edit-mode');
$('.active-edit-mode').toggleClass('edit-mode');
$('.my-element.edit-mode').click(function() {
$(this).remove();
});
});
});
.my-element {
color: #007aff;
cursor: pointer;
}
.edit-mode {
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="elements">
<div class="my-element">
this element
</div>
<div class="my-element">
this element
</div>
<div class="my-element">
this element
</div>
</div>
<button class="active-edit-mode">
active edit mode
</button>
Jsfiddle : jsfiddle
You should check this logic in the onClick event handler of .my-element, this would be more clearer.
$(document).ready(function(){
$('.active-edit-mode').click(function(){
$('.my-element').toggleClass('edit-mode');
$('.active-edit-mode').toggleClass('edit-mode');
});
$('.my-element').click(function(){
if ($(this).hasClass('edit-mode')) {
console.log('click removing')
$(this).remove();
}
});
});
.my-element { color: #007aff;cursor:pointer; }
.edit-mode {font-weight:bold;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="elements">
<div class="my-element">
this element
</div>
<div class="my-element">
this element
</div>
<div class="my-element">
this element
</div>
</div>
<button class="active-edit-mode">
active edit mode
</button>
It will work if you bind the removal function independently. Your JS would look like:
$(document).ready(function(){
$('.active-edit-mode').click(function(){
$('.my-element').toggleClass('edit-mode');
$('.active-edit-mode').toggleClass('edit-mode');
});
});
$(document).on('click','.my-element.edit-mode',function(){
$(this).remove();
});

Jquery hover on div, add styles on grandparent

I have a big problem and I cant find the solution...
I must do the tooltip, for example we have this structure:
<body>
**<div class="tooltip">Long text ...</div>**
<div style="overflow:hidden">
<div class="box">
**<h1>Short text</h1>**
</div>
</div>
</body>
And if I am on h1 I want add to div with class tooltip visibility:visible, how can I do this?
I hope you find the solution to this problem.
You can use jquery .hover() for as h1 and .prepend() for adding div with class .tooltip and visibility: visible something like below
$('h1').hover(function() {
var tooltip = document.getElementsByClassName('tooltip');
if(!tooltip[0]) {
$('body')
.prepend('<div class="tooltip" style="visibility:visible;">Long text ...</div>');
}
});
.tooltip {
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
****
<div style="overflow:hidden">
<div class="box">
**<h1>Short text</h1>**
</div>
</div>
</body>
Hope this will help you in some way(y).
Since your tooltip isn't a parent of the h1 you've to go up two levels then get the previous element with the class tooltip :
$('h1').parent().parent().prev('.tooltip').css({'visibility':'visible'});
You could use hover() method to toggle the visibility :
$('h1').hover(function(){
$(this).parent().parent().prev('.tooltip').css({'visibility':'visible'});
},function(){
$(this).parent().parent().prev('.tooltip').css({'visibility':'hidden'});
})
Hope this helps.
$('h1').hover(function(){
$(this).parent().parent().prev('.tooltip').css({'visibility':'visible'});
},function(){
$(this).parent().parent().prev('.tooltip').css({'visibility':'hidden'});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tooltip" style="visibility:hidden">Long text ...</div>
<div style="overflow:hidden">
<div class="box">
<h1>Short text</h1>
</div>
</div>
When you hover to H1 and you want to add tooltip to the grand parent this will be the answer.
$('.box h1').mouseenter(function() {
$(this.parentNode.parentNode.previousElementSibling).addClass('tooltip');
});
//2nd option
$('.box h1').mouseenter(function() {
$(this.parentNode.parentNode.previousElementSibling).css('visibility', 'visible');
});
Read this to know what target you want to use the event/script W3schools.com

How to check if toggle function show item?

I have this code ...
<div class="btn1">Button</div>
<div class="text1">Some text</div>
...
$(document).ready(function(){
$(".btn1").click(function(){
$(".text1").toggle();
});
});
<div class="block2">Some text 2</div>
I want to hide ".block2" when toggle function show ".text1" and show ".block2" when ".text1" is hidden. How can I do that? I hope my question is clear. Thanks for answers.
1) see this http://jsfiddle.net/a6kqvLj8/
Just make the second block diaplay:none, and do the same operations
on it
$(document).ready(function() {
$(".btn1").click(function() {
$(".text1").toggle();
$(".block2").toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="btn1">Button</div>
<div class="text1">Some text</div>
<div class="block2" style='display:none;'>Some text 2</div>
2) Or if you want the both blocks to be visible at first, and the
start toggling on click, you can do this:
http://jsfiddle.net/a6kqvLj8/1/
$(document).ready(function() {
$(".btn1").click(function() {
$(".text1").toggle();
if ($(".text1").css('display') == 'none') {
$(".block2").css('display', 'block');
} else {
$(".block2").css('display', 'none');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="btn1">Button</div>
<div class="text1">Some text</div>
<div class="block2">Some text 2</div>

show image on hover just for the actual hovered element

I have a set of divs built like this with display:none; on .imageholder:
<div class="parent">
<div class="imageholder">
<img src="img.jpg">
</div>
<div class="parent">
<div class="imageholder">
<img src="img.jpg">
</div>
</div>
<!--and so on...-->
and this jQuery:
$('.parent').hover(
function() {
$('.imageholder').fadeIn('slow');
},function() {
$('.imageholder').fadeOut('slow');
}
);
When I hover the .parent div all related parent divs are displaying the image.
How can I make the hover state to work just for the actual hovered parent element?
Thanks!
function () {
$(this).find(".imageholder").fadeIn("slow");
}
You can use this in the .hover callback to refer to the hovered (parent) element.
$('.parent').hover(function () {
$(this).find(".imageholder").fadeIn("slow");
});
Try this fiddle you can use .find() method for to point div.
Just like
$(document).ready(function()
{
$('.parent').hover(function() {
$(this).find('.imageholder').fadeIn('slow');
},function() {
$(this).find('.imageholder').fadeOut('slow');
})
});

Categories

Resources