How I hide a div when jquery show another div? - javascript

I want that when the click activate the element2 div, the element should disappear. And the element2 div should not appear at the beginning.
$(".toggle").click(function() {
$(".element2").toggle();
});
$(".close").click(function() {
$(".element2").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">
Element 1
<div class="toggle">
toggle
</div>
<div class="element2">
Element 2
<div class="close">close Element 2</div>
</div>
</div>

Add display none to hide an element from the start:
<div class="element2" style="display:none">
The rest of your code appears to be doing what it's supposed to, unless I am misunderstanding "I want that when the click activate the element2 div, the element should disappear"... which is entirely possible.

In order to have element2 hidden at the beginning you need to either add a style tag or even better add a CSS file where you will keep all of your stylings in one place.
For style tag:
<div class="element2" style="display:none">
For CSS:
.element2 {
display: none;
}
Then for your code you are close. In order to make element hide, you need to change it to:
$(".toggle").click(function() {
$(".element2").show();
$(".element").hide();
});
$(".close").click(function() {
$(".element2").hide();
$(".element").show();
});
The HTML will need some changes to, this will make what I wrote work the way I believe you want it to:
<div class="element">
Element 1
<div class="toggle">
toggle
</div>
</div>
<div class="element2">
Element 2
<div class="close">close Element 2</div>
</div>

You should probably do something like this:
$(".toggle").click(function() {
$(this).parent().find(".element2").toggle();
});
$(".close").click(function() {
$(this).parent().hide(); // close the correct .element2
});
In CSS you need to:
.element2 {
display: none;
}

just $(".element2").hide(); hide it at start
$(function() {
$(".element2").hide();
$(".toggle").click(function() {
$(".element2").toggle();
});
$(".close").click(function() {
$(".element2").hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">Element 1
<div class="toggle">Toggle </div>
<div class="element2"> Element 2
<div class="close"> close</div>
</div>
</div>

HTML
<div class="element">
<div class="toggle"></div>
<div class="element2" style="display:none">
<div class="close"></div>
</div>
</div>
EXAMPLE CSS
.toggle
{
display:block;
width:20px;
height:20px;
margin-left:10px;
float:left;
background:green;
}
.element2{
display:block;
width:40px;
height:40px;
margin-left:10px;
float:left;
background:yellow;
}
.close{
display:block;
width:20px;
height:20px;
margin-left:10px;
float:right;
border:1px solid #000;
}
JQUERY
$(".toggle").click(function() {
$(".element2").toggle();
});
$(".close").click(function() {
$(".element2").css({"display":"none"});
});
fiddle to check
I hope it is helpfull answer. Good Luck !

Related

click on parent except one of its child - jquery

Here is my fiddle link.
I want to do some actions on clicking the parent box, except its one child with class .notClickableCol. I used :not(), but currently it is not working.
It is working when this text ("Not Clickable") is without <span> tag.
Any ideas?
HTML/CSS/Jquery:
jQuery(".row").on('click', ':not(.notClickableCol)', function() {
alert('success');
});
.row {
margin-bottom:15px;
border-bottom:1px solid #ccc;
}
.row {
width:150px;
height:150px;
display:inline-block;
vertical-align:top;
text-align:center;
color:#fff;
font-family:Arial;
font-weight:bold;
margin-right:10px;
background:#ff6777;
margin-left:50px;
}
.clickableCol {
display:block;
cursor:pointer;
margin-top:45px;
margin-bottom:20px;
color:#000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col">
<span class="clickableCol">Clickable</span>
</div>
<div class="col">
<span class="notClickableCol">Not Clickable</span>
</div>
</div>
<div class="row">
<div class="col">
<span class="clickableCol">Clickable</span>
</div>
<div class="col">
<span class="notClickableCol">Not Clickable</span>
</div>
</div>
<div class="row">
<div class="col">
<span class="clickableCol">Clickable</span>
</div>
<div class="col">
<span class="notClickableCol">Not Clickable</span>
</div>
</div>
Your selector is too broad. I'd use .col instead of .row:
jQuery(".col :not(.notClickableCol)").on("click", function () {
alert('success');
});
Here's a working fiddle.
You could also just just clickableCol and drop the .not() all together:
jQuery(".clickableCol").on("click", function() {
alert('success');
});
Here's another working fiddle.
Why don't you rather focus on the clickable element? like this:
jQuery(".row").on('click', '.clickableCol', function () {
alert('success');
});
You can do it like following.
jQuery(".row").click(function (e) {
if (!$(e.target).hasClass('notClickableCol')) {
alert('success');
}
});
UPDATED FIDDLE
Instead of excluding only the notclickablecol, you can tell it to only register when it has the class clickablecol using the has selector:
/* For a clickable col inside this row, not every clickableCol
that might be in another table */
jQuery(".row").on('click', ':has(.clickableCol)', function () {
alert('success');
});
You could just use an onclick event on the .clickableCol class, but this isn't nice because you might want another action when you click another col with this class.
Should describe not span with defined class ...
jQuery(".row").on('click', 'span:not(.notClickableCol)', function() {
alert('success');
});

How to get the text value from divs with same class, and to display in another div?

Example here: http://codepen.io/agentmi6/pen/JoZZWm
how can i display the text of the green div in the grey div, when i click on the [+] icon? Ive tried many different scenarios but none works, can someone give me few pointers how can i do this, i would really appreciate.
<div id="wrapper">
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
this is the 1st element
</div>
<div class="first" id="s">
<div class="set">+</div>
this is the 2nd element
</div>
<div class="first" id="t">
<div class="set">+</div>
this is the 3rd element
</div>
</div>
<div id="cc"></div>
</div>
CSS
.first{
margin-top:5px;
border:1px solid black;
width:190px;
height:40px;
background-color:green;
}
#cc{
margin-top:5px;
width:190px;
height:40px;
border:1px solid black;
background-color:grey;
}
.set{
cursor:pointer;
color:#fff;
font-size:33px;
float:right;
border:1px solid white;
}
You'll have to get the text of the .first div, i suggest you put the text into a tag so it'll be easy to get it, and so you don't get the + of the .get button.
Here's the new JavaScript
$(document).ready(function(){
$('.set').click(function(){
// Get the text inside the span in the parent .first of the clicked .set button
var text = $(this).parent('.first').find('span').text();
$('#cc').text(text);
});
});
The HTML would look like this
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
<span>this is the 1st element</span>
</div>
<div class="first" id="s">
<div class="set">+</div>
<span>this is the 2nd element</span>
</div>
<div class="first" id="t">
<div class="set">+</div>
<span>this is the 3rd element</span>
</div>
</div>
<div id="cc"></div>
Here's an updated CodePen
You can use this:
$(document).ready(function(){
$('.set').click(function(){
var text = $(this).parents("div.first").text();
$("#cc").text(text);
})
});
This Code could help you
var tempText="";
$('.container .set').each(function(){
tempText=tempText+ $(this).html();
});
$('#cc').html(tempText);
Here try this (fiddle: http://jsfiddle.net/nek9fona/)
JQ:
$(function(){
$('.set').click(function(){
var $this = $(this);
var text = $this.parent('.first').text();
$('#cc').text(text);
});
});
Put this in your head html section. Or bottom of body section.
<script>
$(document).ready(function() {
$('.set').on('click', function(){
var text_val = $(this).closest('div.first').text();
$('#cc').text(text_val);
});
});
</script>
I guess that you don't want the copied text to contain "+" symbol? In this case you can do this:
$('.set').click(function() {
var text = $(this.nextSibling).text().trim();
$('#cc').text(text);
});
Demo: http://codepen.io/anon/pen/MYXXrr
Note, that it makes sense to improve HTML structure a little by wrapping the text into some container:
<div class="first" id="s">
<div class="set">+</div>
<div class="text">this is the 2nd element</div>
</div>
In this case, code would become much more reliable and cleaner:
$('.set').click(function() {
var text = $(this).next('.text').text(); // or $(this).parent().find('.text')
$('#cc').text(text);
});
Demo: http://codepen.io/anon/pen/ogyyoO

Add icons inside textbox area onfocus event, using bootstrap and javascript

Does anybody knows how can I add some bootstrap icons to my textarea. Icons should show on focus and hide on focus out. This textarea is similary, if not the same as Facebook Add new status textarea.
All I was able to do is to expand and shrink textarea onfocus/focusout events, using JavaScript.
This is the code I have:
HTML:
<div class="jumbotron" style="height:150px">
<div class="col-lg-12">
<textarea class="expand" rows="3" cols="20"></textarea>
</div>
</div>
JS:
<script type="text/javascript">
$('textarea.expand').focus(function () {
$(this).animate({ height: "4em" }, 500);
});
$('textarea.expand').focusout(function () {
$(this).animate({ height: "2em" }, 500);
});
</script>
CSS:
.expand
{
height: 2em;
width: 50%;
}
I've also try using .css in JS and trying to add icon, but I suppose I don't do this right way, cause nothing gets shown.
$('textarea.expand').focus(function () {
$(this).animate({ height: "4em" }, 500);
$(this).css('glyphicon glyphicon-camera');
});
Textarea should behave like this:
OnFocusOut:
OnFocus:
Can someone help me, and give me an idea on how to do this...Because I'm pretty bad in JS.
One Simple answer can be creating a sibling div element to the textarea which will contain all the icons, show the div of focusing the textarea and hide it on focusout of the textarea
This example is using the selector
.parent:hover .links{}
but could be edited to
.parent:focus .links{}
if desired. However, to show you this in action, i've designed a simple demo below:
.parent{
height:100px;
width:70%;
background:red;
position:relative;
}
.parent:hover .links{
display:block;
}
.links{
display:none;
position:absolute;
bottom:0;
}
<div class="parent">
<div class="child">
<div class="links">
facebook twitter etc
<img src="http://placekitten.com/g/20/20" alt="" />
</div>
</div>
</div>
An example using Sibling selectors
Here is a working example of using the sibling selector
input:focus + .items
which selects the class .items of which has a sibling of 'input' which has a focus (i.e. what you're looking for)
#parent{
height:100px;
width:80%;
background:red;
position:relative;
}
.items{
bottom:0;
padding:5px;
display:none;
}
input:focus + .items{
display:block;
}
<div id="parent">
<input type="text" placeholder="enter text"/>
<div class="items">
<img src="http://placekitten.com/g/20/20" alt=""/>
<img src="http://placekitten.com/g/20/20" alt=""/>
<img src="http://placekitten.com/g/20/20" alt=""/>
</div>
</div>

onClick div changes color and back to color when clicking on the other

I want to point out which div I clicked on to prevent confusing as the pop up menu that comes out of it on my site will be both the same just different links but same buttons. Now my problem is that i cant figure out how i can manage to do it been stuck on it for quite some time now. My site is in Wordpress for the info. The color of the text doesnt really matter atm.
The code does work but i want that when i hit "juicyplants" the color changes and when i hit "leafyplant" that color changes and "juicyplants" go back to normal. with the changing colors i mean the already presenting ones.
My code:
HTML:
<div id="clickOne" class="clickDesign">
<h2 class="fs20 nobold">Leafy Plants</h2>
</div>
<div id="clickTwo" class="clickDesign">
<h2 class="fs20 nobold">Juicy Plants</h2>
</div>
</div>
<div id="leafyPlants">
Leafy Test
</div>
<div id="juicyPlants">
Juicy Test
</div>
CSS:
#leafyPlants{
display:none;
position:absolute;
top:50px;
cursor:pointer;
}
#juicyPlants{
display:none;
position:absolute;
top:50px;
cursor:pointer;
}
h2 {
float:left;
display:block;
height:40px;
width:150px;
cursor:pointer;
}
jQuery:
$("#clickOne").on('click', function() {
$("#leafyPlants").fadeIn();
$("#juicyPlants").fadeOut();
});
$("#clickTwo").on('click', function() {
$("#leafyPlants").fadeOut();
$("#juicyPlants").fadeIn();
})
Best to change classes and use styling:
jsfiddle: http://jsfiddle.net/TrueBlueAussie/Pcn5a/2/#update
$(function () {
$("#clickOne").on('click', function () {
$('.clickDesign').removeClass("active");
$(this).addClass("active");
$("#leafyPlants").fadeIn();
$("#juicyPlants").fadeOut();
});
$("#clickTwo").on('click', function () {
$('.clickDesign').removeClass("active");
$(this).addClass("active");
$("#leafyPlants").fadeOut();
$("#juicyPlants").fadeIn();
})
});
in style add:
.active{
color: green;
}
It is better to data-drive any menu system and reduce the code:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/Pcn5a/3/
JQuery:
$(function () {
$('.clickDesign').on('click', function () {
var $this = $(this);
$('.clickDesign').removeClass("active");
$this.addClass("active");
// Use the id in the data-target attribute
$target = $($this.data('target'));
$(".target").not($target).fadeOut();
$target.fadeIn();
});
});
Html (has data-target attributes)
<div>
<div id="clickOne" class="clickDesign" data-target="#leafyPlants">
<h2 class="fs20 nobold">Leafy Plants</h2>
</div>
<div id="clickTwo" class="clickDesign" data-target="#juicyPlants">
<h2 class="fs20 nobold">Juicy Plants</h2>
</div>
</div>
<div id="leafyPlants" class="target">Leafy Test</div>
<div id="juicyPlants" class="target">Juicy Test</div>

Onclick toggle selected div's class and change unselected divs' classes too

here's what I have so far.
http://jsfiddle.net/nSfWs/
Right now when a user clicks on a product div, the green box appears by adding the class "selected" to the product div. What I'd like to also happen is for the class "unselected" to be added remaining two unselected product divs. Therefore, one div would have the green box/border and the other two would be faded with the opacity filter.
Can someone help me make this work? It seems simple enough, but it's driving me crazy. Thanks!
And for those who don't want to click on the jsfiddle link, here's the code.
<style type='text/css'>
div.product {
display:inline-block;
vertical-align:top;
text-align:center;
width:auto;
margin:0 47px 0 0;
padding:24px 22px 20px 27px;
border:1px solid transparent;
}
div.product:last-child {
margin:0px;
}
div.product:hover {
border:1px solid #878787;
-moz-border-radius:3px;
border-radius:3px;
}
div.product.unselected {
opacity:0.6;
filter:alpha(opacity=60);
}
div.product.selected {
border:1px solid #32a24e;
-moz-border-radius:3px;
border-radius:3px;
}
</style>
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(".product").click(function () {
$(this).toggleClass("selected");
});
});//]]>
//]]>
</script>
<div class="product">
<div class="itemImage"><img src="http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png" alt="Model 11710" width="85" height="146" /></div>
<div class="description">1</div>
</div>
<div class="product">
<div class="itemImage"><img src="http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png" alt="Model 11710" width="85" height="146" /></div>
<div class="description">2</div>
</div>
<div class="product">
<div class="itemImage"><img src="http://upload.wikimedia.org/wikipedia/en/thumb/0/0f/Plectrophenax_nivalis1.jpg/320px-Plectrophenax_nivalis1.jpg" alt="Model 11710" width="85" height="146" /></div>
<div class="description">3</div>
</div>
Simply remove the class from the siblings:
$(".product").click(function () {
$(this).toggleClass('selected').siblings().removeClass("selected");
});
just remove the class from other divs on click event,
$(window).load(function(){
$(".product").click(function () {
$(".product").removeClass("selected")
$(this).toggleClass("selected");
});
});
and you can toggle the other class as well,
$(".product").click(function () {
$(".product").not(this).removeClass("selected").addClass('unselected')
$(this).toggleClass("unselected").toggleClass("selected");
});
See I have edited
Try using siblings() to change the class of the remaining divs.
$(this).siblings("product").addClass("unselected");
Try this:
$(".product").click(function () {
$(".product").removeClass("unselected");
$(".product").removeClass("selected");
$(this).addClass("selected").siblings().addClass("unselected");
});
Fiddle
Try this
$(window).load(function(){
$(".product").click(function () {
$(".product").removeClass("selected").addClass("unselected");
$(this).removeClass("unselected").addClass("selected");
});
});
Fiddle

Categories