Show and hide a div with the click of a button

As I embark on rebuilding a website from the ground up, I find myself pondering how to achieve a specific functionality:

My goal is for another view to appear when one of two buttons is clicked. How can this be accomplished?

I attempted the following approach: http://jsfiddle.net/7bqjm1dy/

$("#b1").click(function() {
                $("#first").slideDown();
                $("#sec").slideUp();
            });
            $("#b2").click(function() {
                $("#sec").slideDown();
                $("#first").slideUp();
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
            <div style="vertical-align:top; display:inline;">
                <button id="b1">Click 1</button><br />
                <button id="b2" >Click 2</button>
            </div>
            <div>
                <div id="first" style="display:none; background-color:red; width:100px; height:200px;"></div>
                <div id="sec" style="display:none; background-color:blue; width:100px; height:200px;"></div>
            
            </div>

However, the implemented solution does not meet my expectations. I envision a slideFromLeft effect and desire proper alignment of the button and map.

Answer №1

I've made some updates to your fiddle:

HTML

<div style="text-align:center; display:block;" class="controls">
    <button id="btn1">Press 1</button><br />
    <button id="btn2" >Press 2</button>
</div>
<div class="sections">
    <div id="firstSection" style="background-color:green; width:150px; height:250px;"></div>
    <div id="secondSection" style="display: none; background-color:yellow; width:0px; height:250px;"></div>    
</div>

Javascript

$("#btn1").click(function() {
    $("#secondSection").animate({'width': 0}, 'fast', function(){ 
        $(this).hide(); 
        $("#firstSection").show().animate({'width': '150px'});
    });        
});
$("#btn2").click(function() {
    $("#firstSection").animate({'width': 0}, 'fast', function(){ 
        $(this).hide(); 
        $("#secondSection").show().animate({'width': '150px'});
    });     
});

CSS

.sections, .controls{
    float: right;
}

Check out the demo here!

Answer №2

What do you think of this alternative approach?

JSFIDDLE

$( "#button1" ).click(function() {
    $( "#element1" ).animate({
        width: "100px",
    }, 1500 );
    $( "#element2" ).animate({
        width: "0px",
    }, 1500 );
});

Answer №3

After taking Deer-Outdoor.nl's advice and examining the DOM structure provided, I believe you now have your solution.

<div style="vertical-align:top; display:inline;float:left;">
    <button id="b1">Click 1</button><br />
    <button id="b2" >Click 2</button>
</div>
<div style="float:left">
    <div id="first"></div>
    <div id="sec"></div>
</div>

Notice that those divs now have a float:left style attribute. You can also add some margin to the second div to create space between them.

FIDDLE

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to overlay a div onto a Google Map?

Does anyone know of a plugin or method that can achieve this? I have a website at this link, and at the bottom there is a map. I'm looking to add a small overlay box on top of the map with contact information inside. Edit: The box should be position ...

Ensure that HTML5 form fields are validated dynamically to make them mandatory

I have a form created using Bootstrap that contains several fields. By default, the first and second fields are required. Now, if the third field is filled out, the fourth field becomes mandatory. Similarly, if the fourth field is filled out, the third fi ...

Using .push() with JSON leads to overwriting existing arrays instead of appending new arrays to my JSON variable

I am currently working on incorporating various user input variables into a JSON array. Each element in the array contains multiple variables that are entered by the user. var Items = { id: `${ID}`, datestart: `${datestart} ...

Log into your account by choosing a button using Selenium with Python

While attempting to access my account via this specific link on the Market Watch webpage using Python and Selenium, I encountered a roadblock. Despite successfully selecting the "Sign In" button, no action is triggered, preventing me from entering the desi ...

modify hidden input values with a dropdown menu and the power of jquery

Hey there, I could really use some help with a challenge I'm facing. I'm currently working on an ecommerce website for my girlfriend and incorporating foxycart for handling the cart functionality. However, I'm stuck at a point where I need t ...

Difficulties encountered when attempting to use a background image for shadow effects within a CSS design

I have been learning from a tutorial on how to create a fixed tabless CSS layout, starting from Photoshop and ending with HTML+CSS code. Here is the final example suggested by the tutorial (how it should look like in the end): And this is my own version ...

Causes for the display of the text within the alt attribute

Recently, I attempted to view an HTML page in text browsers and noticed that the alt attribute value of the img tag was visible. I decided to omit the alt attribute and instead utilized CSS: .headerImg:after, .headerImg::after { conte ...

Conceal the div until the animation with a delay of `1s` has finished, then reveal it afterwards

I have incorporated a CSS file to introduce animations on the website I am currently developing. You can find it here: The premise of this CSS file is straightforward - by adding specific class names to div elements, you can animate them as you scroll up ...

What is the best way to convert this PHP string into an image?

Is there a way to replace the string "Overview" with an image instead? If it's possible, how can I achieve this? if (empty($lang) || !is_array($lang)) { $lang = array(); } $lang = array_merge($lang, array( 'UCP_MAIN' => 'Ov ...

Can $refs cause issues with interpolation?

I'm currently learning Vue.js and the course instructor mentioned that modifying the DOM element using $refs should not affect interpolation. In fact, any changes made directly to the DOM will be overridden by interpolation if it exists. However, in m ...

Is there a way to determine if a button was clicked using twig?

I need assistance with implementing a button in my twig file within a table that will remove an element from an array. Ideally, I would like to remove the element based on its index. From the research I have conducted, it seems that data manipulation shou ...

Is there a way to do HTML select-option in a reversed order?

Having an issue with my HTML select element: <select> <option>...</option> <option>...</option> <option>...</option> </select> Currently, all the options are displayed below the select field. I ...

Utilizing AJAX Requests in jQuery Widget Elements for an Interactive Dashboard

I recently implemented the jQuery Dashboard plugin on my site to showcase information from a MySql table. Each widget contains buttons corresponding to the results, which when clicked trigger an ajax call to fetch and display a styled form in a jQuery dial ...

Positioning CSS for a Responsive Button

Currently, I am working on making my modal responsive. However, I am encountering an issue with the positioning of the "SAVE" button. The button is not staying in the desired position but instead disappears. Below is the snippet of my HTML and CSS: .dele ...

What is the best method for saving a chosen radio button into an array?

I am currently developing an online examination system where questions are retrieved from a database using PHP and displayed through AJAX. I am facing an issue where I am unable to capture the selected radio button value and store it in an array. Despite e ...

Use Javascript to conceal a div element if there are no links present

I am working on a website using ModX CMS and I am attempting to hide or remove a div element when it does not contain any anchor tags. How can I achieve this? I have already attempted the following code without success: jQuery(function($) { if ($(".pages ...

When I include the alert('it is functioning now'); function, it operates correctly, however, it is not desired in this situation

After adding alert('now it works') to this function, it only functions correctly. However, I do not want to include this alert but the function fails without it. function a() { var ac = document.forms["myForm"]["textfield"].value; $.ajax ...

Creating a scheduled redirect button using JavaScript and another button to cancel it

I'm facing an issue with my code. When the first button is clicked, it redirects the user to a different URL after 10 seconds. However, I'm struggling to make the second button cancel the redirect if pressed before the 10 seconds are up. Despite ...

Switch up the appearance of a document by manipulating the stylesheet using a select tag

I'm currently facing an issue with the implementation of a drop-down box on my website for selecting different themes. Despite having the necessary javascript code, I am unable to get it working correctly. Here's the snippet: //selecting the sele ...

What is the best way to begin an ordered list from the number 2 instead of 1, while ensuring W3C validity and compatibility with

Is it possible to start an ordered list from 2 instead of 1? I need the solution to be compatible with all browsers, including IE6, and ensure that the HTML and CSS are valid. ...