Jquery Modal on click add class then remove - javascript

This bit of code is supposed to add a class and then with a delay remove the div entirely. It just jumps to deleting the div. Any ideas why?
The HTML
<!-- modal conent-->
<div class="modal" id="modal">
<div class="modal-content ">
<h1>Tricks Are 4 Kids</h1>
<p>Ok so my modal doesn't look like this one, but this is it boiled down. Can I write the JS better??? ....</p>
<button href="#" class="close" id="close" >X</button>
</div>
</div>
</div>
<!-- page conent-->
<div id="page-content-wrapper" >I am a div with all the page conent. lalalalala</div>
The CSS
.red {background:red;}
The jQuery
$(document).ready( function() {
$( "#close" ).click(function() {
$("#modal").addClass("red").delay(800).queue
$("#modal").remove();
});
Codepen https://codepen.io/Ella33/pen/GjQZRP

$(document).ready( function() {
$( "#close" ).click(function() {
$("#modal").addClass("red");
setTimeout(function() {
$("#modal").remove();
}, 800);
});

Related

Is this a stable way to show/hide a div and a certain "id" using jQuery or could I use a better approach?

I have two containers, one is a page of link blocks and a have another container hidden. In the hidden container there is articles that link to the main pages link blocks. So user "clicks" LINK ONE in the main container which then hides the main container, shows the originally hidden container and ONLY displays the id article it is linked to. There is then a "back to library" button which will take you back to the main container and set everything back to square one. I have it working at the moment but I'm not sure if it is the right way to achieve this? I know there is no right or wrong and if it works, it works but just wondering for peace of mind. Thanks :)
CODEPEN: https://codepen.io/mDDDD/pen/ZEObRdR
HTML:
<div class="container--article-blocks" id="articleBlocks">
<div class="article-block">
<a class="show-article" href="#articleOne">LINK ONE</a>
</div>
<div class="article-block">
<a class="show-article" href="#articleTwo">LINK TWO</a>
</div>
<div class="article-block">
<a class="show-article" href="#articleThree">LINK THREE</a>
</div>
</div>
<!-- articles -->
<div class="container--articles" id="articles" style="display: none;">
back to Library
<div id="articleOne" class="article-block-articles">One</div>
<div id="articleTwo" class="article-block-articles">Two</div>
<div id="articleThree" class="article-block-articles">Three</div>
</div>
jQuery:
$('.show-article').on('click', function (e) {
e.preventDefault();
var id = $(this).attr('href');
$(id).show().siblings('div').hide();
$('#articleBlocks').fadeOut();
setTimeout(function () {
$('#articles').fadeIn();
}, 500);
});
$('.back-to-library').on('click', function (e) {
e.preventDefault();
$('#articles').fadeOut();
setTimeout(function () {
$('#articleBlocks').fadeIn();
}, 500);
});
Consider using the callback for .fadeOut() to then call .fadeIn(). Nothing wrong with that you did, this will just ensure the item is faded out before executing further code.
$(function() {
$('.show-article').on('click', function(e) {
e.preventDefault();
var id = $(this).attr('href');
$(id).show().siblings('div').hide();
$('#articleBlocks').fadeOut(500, function() {
$('#articles').fadeIn();
});
});
$('.back-to-library').on('click', function(e) {
e.preventDefault();
$('#articles').fadeOut(500, function() {
$('#articleBlocks').fadeIn();
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container--article-blocks" id="articleBlocks">
<div class="article-block">
<a class="show-article" href="#articleOne">LINK ONE</a>
</div>
<div class="article-block">
<a class="show-article" href="#articleTwo">LINK TWO</a>
</div>
<div class="article-block">
<a class="show-article" href="#articleThree">LINK THREE</a>
</div>
</div>
<!-- articles -->
<div class="container--articles" id="articles" style="display: none;">
back to Library
<div id="articleOne" class="article-block-articles">One</div>
<div id="articleTwo" class="article-block-articles">Two</div>
<div id="articleThree" class="article-block-articles">Three</div>
</div>
You can make the visibility: hidden; after an event!
Actual example
document.getElementById("XXX").style.visibility = "hidden";
But actually, your project is just fine!
For more info, go here https://www.w3schools.com/jsref/prop_style_visibility.asp
This explains the HTML DOM visibility property!

Cannot get HTML/CSS modal box to open up and display on page

Using JQuery I'm trying to let the user select an image that is displayed in on the html page using the below code. This is then displayed in the modal box via #badgeselect
At the moment the modal box is not being opened after the user clicks the image. It looks like it might be trying to place it on the actual main page and not in the modal box.
I can make the box appear successfully using
Demo Show
But I'm wondering if
$(this).width(100).height(100).appendTo('#badgeselect');
is the issue for it not working?
JQuery
$(document).ready(function() {
$('.go img').css('cursor', 'pointer');
$('.go').on('click', 'img', function(e) {
$(this).width(100).height(100).appendTo('#badgeselect');
$('#modal1').reveal({
});
return false;
});
});
Modal box
<!-- A modal with its content -->
<section class="modal--show" id="modal1"
tabindex="-1" role="dialog" aria-labelledby="label-show" aria-hidden="true">
<div class="modal-inner">
<header>
<h2 id="label-show">A modal</h2>
</header>
<div class="modal-content">
<p>Content.</p>
<div id="badgeselect"></div>
<select id="selectFriend">
<option>Select friend</option>
</select>
<textarea id="UploaderComment" rows="5" cols="30">Add a comment</textarea>
<a id="send" class="button green close">
<img src="images/tick.png">Confirm</a>
<a href="http://example.com/page.html" class="button red close">
<img src="images/cross.png">Cancel</a>
</div>
<footer>
<p>Footer</p>
</footer>
</div>
<a href="#!" class="modal-close" title="Close this modal"
data-dismiss="modal" data-close="Close">×</a>
</section>
<!-- END -->
I think you need to load the reveal modal to get the img in the modal popup
$(document).ready(function() {
$('.go img').css('cursor', 'pointer');
$('.go').on('click', 'img', function(e) {
$('#modal1').reveal({ }); return false;
});
$(this).width(100).height(100).appendTo('#badgeselect');
});

js child class toggle

How would you toggle the class of an inner div? example:
html:
<div class="masonry">
<div class="item">
<div class="item-content">1
<div class="item-more-content">1+</div>
</div>
</div>
<div class="item">
<div class="item-content">2</div>
</div>
</div>
javascript:
$( function() {
var $container = $('.masonry').masonry({
columnWidth: 60
});
$container.on( 'click', '.item-content', function() {
$( this ).parent('.item').toggleClass('is-expanded');
$( this ).children('.item-more-content').toggleClass('iz-expanded');
$container.masonry();
});
});
.parent works, .children does not.
How to toggle css class of child div, "item-more-content"?
This works fine for me (I would comment but don't have enough points yet). Are you sure you want to toggle class 'iz-expanded' and not 'is-expanded' for '.item-more-content'? - typo?

switch/hide div JQuery

When I expand a division it can't be collapse again. All I need is when the division is shown I want to hide it again when clicking on it.
Here's my HTML:
<div class="login_area" id="clickHere2">
Signup
<div class="drop-down" id="signup_area_hover">
Signup
</div>
</div>
<div class="login_area" id="clickHere">
Login
<div class="drop-down" id="login_area_hover">
Login
</div>
</div>
<div class="drop-down" id="dropdown_login">
<div class="dropdown_login_header">
<div class="beeper_value"></div>
<div class="beeper_login"></div>
</div>Hello World 111
</div>
<div class="drop-down" id="dropdown_signup">
<div class="dropdown_signup_header">
<div class="beeper_signup"></div>
</div>Hello World 2222
</div>
and here's the JS :
<script type="text/javascript">
$( '#clickHere' ).click(function() {
$( '#dropdown_login' ).slideToggle(500);
});
$( '#clickHere2' ).click(function() {
$( '#dropdown_signup' ).slideToggle(500);
});
</script>
I want to add an if condition if the clickHere is shown, ClickHere2 hides and vice versa.
Take a look at the toggle / slideToggle functions in jquery.
http://api.jquery.com/toggle/ and https://api.jquery.com/slideToggle/
There are examples on the bottom of the page.
DEMO
jQuery:
$( "#myBtn" ).click(function() {
$( "#myDiv" ).toggle();
});
HTML:
<div id="myDiv"></div>
<button type="button" id="myBtn">Toggle</button>
CSS:
#myDiv {
background-color:red;
height:75px;
width:75px;
}
Resources:
http://api.jquery.com/toggle/

Javascript - jquery,accordion

<div id="wrapper">
<div class="accordionButton">Personal Information</div>
<div class="accordionContent">
Personal text
</div>
<div class="accordionButton">Experience</div>
<div class="accordionContent">
Experience information
</div>
<div class="accordionButton">Training</div>
<div class="accordionContent">
No skills
<div class="accordionButton">Career</div>
<div class="accordionContent">
Never had a job
</div>
<div class="accordionButton">Referers</div>
<div class="accordionContent">
None.
</div>
</div>
This code works how i want it to. It is a horizontal accordion. However, when it is loaded on my webpage, they content divs have to be hidden for my jquery to work.
Jquery:
$(document).ready(function() {
// When div is clicked, hidden content divs will slide out
$('div.accordionButton').click(function() {
$('div.accordionContent').slideUp('normal');
$(this).next().slideDown('normal');
});
// Close all divs on load page
$("div.accordionContent").hide();
});
If i don't hide the divs, all the divs display. Is there any way to display the 1st page without changing too much of my jquery or would i have to set different class names for the button and the content so that when a specified button is clicked, the affiliated content will slide out for that button div?
You can use jquery selector for first div and set it as show(). Something like :
$('div.accordionContent:first').show();
I think you have to try this:-
<script type="text/javascript">
$.fn.accordion = function(settings) {
accordion = $(this);
}
$(document).ready(function($) {
$('div.accordionContent').click(function() {
if($(this).next().is(":visible")) {
$(this).next().slideDown();
}
$('div.accordionContent').filter(':visible').slideUp();
$(this).next().slideDown();
return false;
});
});
</script>
Why not use slideToggle() instead of IF statements....
Try this very simple solution
HTML
<div class="accordion-container">
<div class="accordion-content">
<div class="accordion-title">Title</div>
<div class="accordion-toggle">Toggle content</div>
</div>
<div class="accordion-content">
<div class="accordion-title">Title</div>
<div class="accordion-toggle">Toggle content</div>
</div>
<div class="accordion-content">
<div class="accordion-title">Title</div>
<div class="accordion-toggle">Toggle content</div>
</div>
</div>
jQuery
$('.accordion-content .accordion-title').on('click', function(){
$(this).toggleClass('active');
$('.accordion-title').not($(this)).removeClass('active');
$(this).next().slideToggle();
$(".accordion-toggle").not($(this).next()).slideUp();
});

Categories