Reloading a larger div after a smaller divs are triggered - javascript

I created three onclick trigger functions that trigger three separate divs in a larger div "main". For some reason, the function only works after one click, then I have to refresh the page.
For example, if I click to trigger the sports function, I have to refresh the page before I can use the news button. From there I have to refresh the page. How do I get the main div to reload before I trigger a second or third event? I've tried location.reload(), but that doesn't seem to work.
<div id="main">
<div id="news">
<h2 id="newsbtn"> News </h2>
</div>
<div id="interactive">
<h2 id="interactivebtn"> Interactive</h2>
</div>
<div id="sports">
<h2 id="sportsbtn"> Sports </h2>
</div>
</div>
$( "#newsbtn" ).click(function() {
$("#sports").fadeOut( "slow", "linear" ),
$("#interactive").fadeOut( "slow", "linear" );})
$( "sportsbtn" ).click(function() {
$("#interactive").fadeOut( "slow", "linear" ),
$("#news").fadeOut( "slow", "linear" );})
$( "#interactivebtn" ).click(function() {
$("#sports").fadeOut( "slow", "linear" ),
$("#news").fadeOut( "slow", "linear" );})
$( "#news" ).click(function() {
$("#sports").fadeOut( "slow", "linear" ),
$("#interactive").fadeOut( "slow", "linear" );})
;})

It sounds like you are basically trying to make an accordian.
That being the case, you're currently hiding all the other elements when one element is clicked. After that, you cant click one of the other elements because you have hidden them.
Generally, for an accordian, each element would have a container with a header and a content area and only the content area would be hidden leaving the header to be clicked
Here is a simple (and not perfect) example, if this is indeed what you are wanting:
$("#main").on('click', '.selectable-container .header', function() {
var $container=$(this).closest('.selectable-container');
$('.selectable-container').not($container).find('.content').fadeOut("slow", "linear");
$container.find('.content').fadeIn("slow", "linear");
})
.selectable-container .content{
display:none;
}
.selectable-container .header{
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<div class="news selectable-container">
<div class="header">news</div>
<div class="content">news content</div>
</div>
<div class="interactive selectable-container">
<div class="header">header</div>
<div class="content">header content</div>
</div>
<div class="sports selectable-container">
<div class="header">sports</div>
<div class="content">sports content</div>
</div>
</div>

Related

Toggle elements view on buttons click

I have different divs which all have the same class "textBox".
At the same time there should always just be one box displayed. For most Boxes there is a button on the bottom of my page which can be clicked and triggers
to make the box visible and hide the box which is already shown at this moment.
Edit: Fiddle https://jsfiddle.net/8uvsq7ta/
For this I have this JS-code:
$( "#gettingStartedButton" ).click( function () {
if (! $( "#gettingStarted" ).is( ":visible" ) ) {
if ( $( "#extension" ).is( ":visible" ) ) {
$( "#extension" ).fadeOut( function () {
$( "#gettingStarted" ).fadeIn();
});
}
else if ( $( "#executingBox" ).is( ":visible" ) ) {
$( "#executingBox" ).fadeOut( function () {
$( "#gettingStarted" ).fadeIn();
});
}
else if ( $( "#feedback" ).is( ":visible" ) ) {
$( "#feedback" ).fadeOut( function () {
$( "#gettingStarted" ).fadeIn();
});
}
else if ( $( "#impressum" ).is( ":visible" ) ) {
$( "#impressum" ).fadeOut( function () {
$( "#gettingStarted" ).fadeIn();
});
}
else if ( $( "#registration" ).is( ":visible" ) ) {
$( "#registration" ).fadeOut( function () {
$( "#gettingStarted" ).fadeIn();
});
}
}
else {
$( "#gettingStarted" ).fadeOut( function () {
$( "#executingBox" ).fadeIn();
});
}
});
The div boxes look like this:
<div id="gettingStarted" class="textBox">
test blabla
</div>
<div id="feedback" class="textBox">
test blabla
</div>
<div id="registration" class="textBox">
test blabla
</div>
<div id="impressum" class="textBox">
test blabla
</div>
CSS:
.textBox {
diplay: none;
}
This Code checks if the box is already shown and if yes it checks EVERY OTHER BOX to get the one which is visible and then fade it out to afterwards fade the reffered box in.
My problem is, I need this part of code for every box. I think there should be a better way to accomplish this.
What I am searching is kind a method openBox(id) where I give the id of the box as paramete and it automatically detects all other boxes with the class parameter and detects which is already faded in, then fades this out to fade the box with the id in.
Sadly my javascript skills aren't that good, so I seek to find some advices or examples how to achieve this.
Thank you very much for every input you can give me.
var $textBox = $(".textBox"); // get 'em all!
$textBox.eq(0).fadeIn(); // FadeIn first one
$("[data-showid]").on("click", function(){ // Buttons click (Use data-* attribute!)
var $box = $("#"+ this.dataset.showid); // Get the target box ID element
$textBox.not($box).hide(); // Hide all bot targeted one
$box.stop().fadeToggle(); // FadeToggle target box
});
.textBox{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gettingStarted" class="textBox">getting started blabla</div>
<div id="feedback" class="textBox">feedback blabla</div>
<div id="impressum" class="textBox">impressum blabla</div>
<button data-showid="gettingStarted">GS</button>
<button data-showid="feedback">FB</button>
<button data-showid="impressum">Imp</button>
If you don't want the current box to toggle, than instead of .fadeToggle() use .fadeIn().
http://api.jquery.com/fadetoggle/
https://api.jquery.com/data/
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
With the addition of a data-* attribute on your button to link it to its content:
<button id="gettingStartedButton" data-link="gettingStarted">GS</button>
<button id="feedbackButton" data-link="feedback">FB</button>
<button id="impressumButton" data-link="impressum">Imp</button>
The javascript becomes very simple:
$( "#gettingStarted" ).fadeIn();
$('button').click(function(){
var link = $(this).data('link');
$('.textBox:visible').fadeOut(function(){
$('#' + link).fadeIn()
});
})
Updated fiddle: https://jsfiddle.net/8uvsq7ta/2/
I did it using class. The HTML would be like :
<div class="textBox gettingStartedButton" style="display: none;"> 1 test blabla </div>
<div style="display: none;" id="feedback" class="textBox .gettingStartedButton gettingStartedButton"> 2 test blabla </div>
<div id="registration" class="textBox gettingStartedButton" style="display: block;">3 test blabla </div>
<div id="impressum" class="textBox"> 4 test blabla </div>
And use the code below. Keep any element visible at first. Then on click of that element it fades it out and fades the next element in. and adds the class "gettingStartedButton" to the visible element to run click event again.
$( ".gettingStartedButton" ).on('click', function () {
var visible_element = $('.textBox:visible');
visible_element.next().fadeIn();
visible_element.next().removeClass('gettingStartedButton');
visible_element.next().addClass('gettingStartedButton');
visible_element.fadeOut();
});

jquery draggable on few divs

Picture:
<div id="grid-wrapper">
<div id="grid"></div>
<div id="grid"></div>
<div id="grid"></div>
<div id="grid"></div>
</div>
I want to change class on two divs when I drop my span, and remove draggable.
I have this:
$( ".dragandrop" ).draggable({ snap: "#grid" });
$("#grid").droppable({
drop: function( event, ui ) {
$( this )
.addClass( "droped" )
}
});
This code add me .droppable only on first div, and doesn't add class "droped", I can't do removing .draggable. Can you help me?
First off, be careful using the same id for multiple elements. It can cause really weird issues (see this post).
Second, make sure you add the draganddrop class to the appropriate elements that you want to, well, drag and drop.
And you want to removed the draggable on an element once you dropped it? I think what you want looks something like this:
$( ".dragandrop" ).draggable({
snap: ".dragandrop" ,
stop: function(event, ui){
$( this ).addClass( "droped" );
$( this ).draggable( 'disable' );
}
});
$(".dragandrop").droppable({});
div{
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div id="grid-wrapper">
<div id="grid1" style="background:orange;" class="dragandrop">a</div>
<div id="grid2" style="background:red;" class="dragandrop">b</div>
<div id="grid3" style="background:green;" class="dragandrop">c</div>
<div id="grid4" style="background:yellow;" class="dragandrop">d</div>
</div>
You may also want to add in some logic to keep the active div on top.

How can I have only one div shown at a time?

I have this code but I'm not sure how to make it so that each time one button is clicked, it closes the other div that is already open. New to jquery!
HTML:
<p class="profile-name">Name</p><br>
<p class="profile-title">Documentation Officer</p><br>
<button id="button-g" class="bio-button">Bio</button><br>
<a class="profile-email" href="mailto:email#test.com">email#test.com</a>
<div class="toggler">
<div id="effect-g" class="profile-bio">
<p>Bio information. Bio Information</p>
</div>
</div>
JQUERY:
<script>
$(function() {
$( "#button-a" ).click(function() {
$( "#effect-a" ).slideToggle( "visible");
});
$( "#button-b" ).click(function() {
$( "#effect-b" ).slideToggle( "visible");
});
$( "#button-c" ).click(function() {
$( "#effect-c" ).slideToggle( "visible");
$("#button-b").hide();
});
$( "#button-d" ).click(function() {
$( "#effect-d" ).slideToggle( "visible");
});
$( "#button-e" ).click(function() {
$( "#effect-e" ).slideToggle( "visible");
});
$( "#button-f" ).click(function() {
$( "#effect-f" ).slideToggle( "visible");
});
$( "#button-g" ).click(function() {
$( "#effect-g" ).slideToggle( "visible");
});
});
</script>
It's rarely wise to target a zillion elements by ID (or any other unique attribute) in a uniform, repetitive structure. Give all your buttons and all your collapsible siblings the same classes, respectively, then do this (or something similar--I can't be more specific without seeing your HTML):
$('.my-button-class').click(function() {
$(this).next('.my-collapsible-div-class').slideDown()
.siblings('.my-collapsible-div-class').slideUp();
});
This assumes markup like this:
<button class="my-button-class">Button</button>
<div class="my-collapsible-div-class"> ... </div>
<button class="my-button-class">Button</button>
<div class="my-collapsible-div-class"> ... </div>
<button class="my-button-class">Button</button>
<div class="my-collapsible-div-class"> ... </div>
Update based on your HTML:
$('.bio-button').click(function () {
$(this).nextAll('.toggler:first').slideToggle()
.siblings('.toggler').slideUp();
});
Demo
http://api.jquery.com/nextall
http://api.jquery.com/first-selector
Off-topic suggestion: Use CSS margin or padding rather than line breaks to format your content. Extra markup elements for spacing is ugly and inefficient.
Give the div's a common class, and a custom data attribute with the letter of the next div to open, then combine this into a single function. Sample div:
<div id="effect-a" class="effect"></div>
Sample button
<button id="button-a" class="button" data-letter="a">Click me</button>
Single function
$(".button").click(function() {
//Slide up any open divs
$(".effect").slideUp();
var divLetter = $(this).data("letter") //a
//Concatenate selector
$("#effect-" + divLetter).slideDown();
});

Using Slider Ui to control a carousel (to go to a certain slide on a certain value)

Good morning all, I have been trying to create this "thing" for a while, but my brain isnt playing along!
I am trying to create a slider feature (using http://jqueryui.com/slider/#steps) where the user selects amount they spend on item, then they click "calculate" which then takes them to a carousel slide which has a info on relevant to item value (its all built with carousel cycle2).
Hope I've explained this ok - I banged up a super quick fiddle here:
http://jsfiddle.net/P4FrJ/
<div class="cycle-slideshow" id="s1"
data-cycle-fx="scrollHorz"
data-cycle-timeout="0"
data-cycle-prev="#prev"
data-cycle-next="#next"
data-cycle-slides="> div"
>
<div class="binbarberbg white">
<div class="container pagewidth">
<div class="col-md-6" id="1" >
<p> <label for="amount">select amount you spend on *item*:</label> <input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;"></p>
<div id="slider"></div>
Calculate
</div>
</div>
</div>
<div id="2" class="slide2">£10</div>
<div id="3" class="slide3">£20</div>
<div id="4" class="slide2">£30</div>
<div id="5" class="slide3">£40</div>
<div id="5" class="slide3">£50</div>
</div>
<div style="height: 200px; clear: both;"></div>
<div class="center">
Prev
Next
</div>
$(function() {
$( "#slider" ).slider({
value:10,
min: 10,
max: 50,
step: 10,
slide: function( event, ui ) {
$( "#amount" ).val( "£" + ui.value );
}
});
$( "#amount" ).val( "£" + $( "#slider" ).slider( "value" ) );
});
$( ".selector" ).slider({
change: function( event, ui ) {}
});
$( ".selector" ).on( "slidechange", function( event, ui ) {} );
You just have to bind a click event handler function on your #calculate link.
This function will retrieve the slider value and make a call to the goto function of the carousel as described in the carousel API:
$('#calculate').click(function(e) {
// Retrieve the slider value (10, 20, 30, etc.), divide by 10
// to get the corresponding carousel slide index (0 based)
// and use the 'goto' function (cf. http://jquery.malsup.com/cycle2/api/)
$('#s1').cycle('goto', $('#slider').slider('value')/10);
});
I updated your jsFiddle to show it in action.

Jquery Expand Collapse

I am having trouble with jquery expand and collapse.
I am planning to have a read more function over my site and i want to make use of this example.
http://www.designgala.com/demos/collapse-expand-jquery.html
if you could see clicking the header expands the content and clicking twice gives a result of minimizing but my issue here is to show some part of the content like 30px height from the div and when the header is clicked it would expand and show the whole content.
I created a jsfiddle which shows a way to do what you are looking to do.
http://jsfiddle.net/cSvAB/2/
Here is the javascript:
$( '.section' ).each( function( index, value ){
$( value ).data( 'height', $( this ).height() );
$( value ).css( {height: 30} );
});
$( '.section .header' ).click( function(){
if( $( this ).data( 'expanded' ) ){
$( this ).parent().animate( {height: 30} );
$( this ).data( 'expanded', false );
}else{
$( this ).parent( ).animate( {height: $( this ).parent().data( 'height' )} );
$( this ).data( 'expanded', true );
}
});
and here is the HTML:
<div class="section">
<div class="header">Click to read more</div>
<div>Lots of text here</div>
</div>
<div class="section">
<div class="header">Click to read more</div>
<div>Lots of text here</div>
</div>
The only other important thing is that .section is given the css attribute overflow: hidden;
.section{ overflow: hidden; }
What you want is actually an Accordion. You can use jQuery UI's accordion if you want a more professional look. Here it is: jqueryui.com/accordion/

Categories

Resources