Activate jQuery when a click event occurs to modify the CSS of a specific

I've managed to create 2 buttons labeled SHOW & HIDE, along with a DIV containing some content. My goal is for the first button to expand the width of the content DIV upon clicking, hiding itself in the process and revealing the second button simultaneously.

Conversely, when the second button is clicked, the content DIV should collapse its width, hiding the second button and bringing back the first button. However, I'm encountering an issue with this functionality. Can someone help me out?

$(document).ready(function(){
    $("#seeall2").hide();

    $("#seeall").bind("click",
    function(){
        $("#speakers").animate({width: 880}, 1500);
        $("#seeall").hide(500);
        $("#seeall2").show(500);
    });

    $("#seeall2").bind("click",
    function(){
        $("#speakers").animate({width: 410}, 1500);
        $("#seeall").show(500);
        $("#seeall2").hide(500);
    });
});

This is my HTML :

<div id="speakers">

          <a href="" id="seeall">EXPAND</a>
          <a href="" id="seeall2">COLLAPSE</a>


            <div id="pics-wrap">
 content...
            </div>
</div>

PLEASE SEE THIS http://jsfiddle.net/ajinkyax/mSjbV/ ITS WORKING NOW. SOLUTION : I removed extra space from SCRIPT tag

Answer №1

This code has been tested and is working properly:

$(document).ready(function () {

    $("#seeall2").hide();

    $("#seeall").bind("click", function () {
        $("#speakers").animate({
            width: 880
        }, 1500);
        $("#seeall").hide(500);
        $("#seeall2").show(500);
    });

    $("#seeall2").bind("click", function () {
        $("#speakers").animate({
            width: 410
        }, 1500);
        $("#seeall").show(500);
        $("#seeall2").hide(500);
    });
}); 

Link to the working demo on jsfiddle

Answer №2

Experience a fully functional webpage....

<html>
<head>
    <title>Homepage</title>
</head>
<body>

<button id="showContent" value="Display" type="button">
    Display</button>
<button id="hideContent" value="Hide" type="button">
    Hide</button>
<div id="contentSection" style="display: none">
    [Your Content]
</div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script type="text/javascript">
    $(document).ready(function (e) {
        $("#hideContent").hide();
        $("#showContent").bind("click",
                function (e) {
                    $("#contentSection").show().animate({ width: 880 }, 1500);
                    $("#showContent").hide(500);
                    $("#hideContent").show(500);
                });
        $("#hideContent").bind("click",
                function (e) {
                    $("#contentSection").hide().animate({ width: 410 }, 1500);
                    $("#showContent").show(500);
                    $("#hideContent").hide(500);
                });
    });

</script>

</body>
</html>

Enjoy this interactive page!!!

Answer №3

There seems to be an issue with your current setup.

The problem lies in using two anchor tags simultaneously.

Clicking on one anchor tag triggers events for both.

 $("#seeall").bind("click", and   $("#seeal2").bind("click", 

To resolve this, consider using two buttons instead of anchors.

<input type="button" id="seeall" value="EXPAND" />
<input type="button" id="seeall2" value="COLLAPSE" />

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

Steps to add a background image without a border:

I am struggling to insert an image within an anchor tag. <a> <img calss="imageclass"/></a> Whenever I try, a border appears around the image. I have attempted using CSS properties like border: 0 and border-style:none, but nothing seems ...

Numerous submissions in various forms

I am a self-taught programmer with no formal education or experience, so please bear with me for my code... The code below is an attempt to convert a site designed for an iPhone into a single-page site using ajax. The issue I'm facing is with multipl ...

What is the most effective way to send a list of objects to a Controller

https://i.stack.imgur.com/oyi5v.png This form is for billing purposes. You can add the item name, quantity, and price multiple times to calculate the total amount. Once you click on submit, all the included items along with other parameters like bill nu ...

Load a .obj model into a variable using three.js

Just diving into the world of javascript and three.js with a question on my mind. I have successfully loaded a basic .obj file using the following code: var loader = new THREE.OBJLoader(); loader.load( 'test.obj', function ( object ) { scene. ...

Menu navigation with animated transitions - Not achieving the desired result

Recently, I posted this question and received a fantastic answer. However, now my client is requesting a hover animation for the navigation items. I started exploring jQuery Spritely as it seemed like the perfect solution. Unfortunately, despite my efforts ...

JavaScript code for transitioning between a thumbnail and a full-width image header on a webpage

I am looking to create transitions in NextJs that involve a smooth shift from a thumbnail image to a full-screen header when clicking on a project from the home page. I prefer utilizing internal routing rather than React Router, but I am open to using that ...

HTML border width is set to 100%, but there seems to be an issue with the responsive menu border and icon display

One issue I'm encountering with my website involves the border at the bottom of my navigation bar. Despite trying to adjust the box-sizing property to border-box, I still can't get it to display correctly. My goal is to have the border span 100% ...

Struggling with incorporating a lightbox within an accordion feature in an .html file

I'm currently attempting to integrate this lightbox into this accordion feature. Individually, I've managed to get both elements up and running smoothly. However, when trying to combine them on the same page (regardless of nesting one inside the ...

Issues with implementing @font-face on a singular font

I've encountered an issue while setting up a website for a client concerning the fonts. Most fonts are displaying correctly, but there is one font, "chunkfive", that is not working as expected. The problem becomes more severe in Internet Explorer wher ...

How can we stop the anchor jump caused by a third-party script?

I'm currently utilizing a third-party script (details provided below) to divide a form into multiple ajax'd pages. However, when I attempt to move on to the next page, it immediately jumps to an anchor at the top of the form. This behavior is unn ...

Is it possible to implement Ionic components on a website without using the Ionic framework?

Having utilized and appreciated Ionic for many mobile apps, I am now in the process of creating a web client for my application. It has come to my attention that Ionic was not designed for desktop applications. However, I am curious if I can still utilize ...

Customize the Underline Color in MaterialUI Select Component

I'm experimenting with MaterialUI and trying to figure out how to customize the underline and text color of the Select component when an item is selected. Currently, the underline and label appear blue after selection with a gray background. Is there ...

Utilize flexbox for text wrapping while preserving the proportionate column width for varying text lengths

I'm currently working on a Wordpress site, incorporating flexbox into my design. I have set up two columns where child 1 should occupy 75% of the page width and child 2 should take up 25%. However, I've noticed that when the content in child 1 is ...

Preventing Click Events from Firing During Drag in JavaScript

I have implemented a code for dragging containers left or right, which is functioning properly. Users can also click on the "thumb". However, I am facing an issue where a click event occurs even after dragging. I want to ensure that only either drag or cli ...

What is the reason behind certain vanilla JavaScript libraries necessitating NPM?

I am a beginner in JavaScript and I want to learn more about it. Recently, I discovered a library called bounce.js which is an animation library. However, it requires NPM for installation, but I am hesitant to use NPM or any packet Manager because they hav ...

What is the best way to achieve this particular look using CSS3?

Seeking assistance with creating a CSS3 layout similar to the one shown in the image below: CSS3 layout Currently, I have managed to center a square on the screen using the following HTML and CSS: HTML: <div id="divBorder"> </div> CSS: #d ...

Removing information from a datatable using an AJAX request

I am facing an issue with my AJAX call in a view. The code for the AJAX request involves hitting a method on the backend: <script> // deleting $("#send").click( function sender() { $.ajax({ contentType: 'application/json ...

What is the best way to prevent 2 divs from overlapping on a webpage?

I'm having trouble getting the smaller div to display right below the larger red div. I tried using the display block css property, but it doesn't seem to be working as expected. Unfortunately, I can't provide an image here, but you can vie ...

What could be causing the issue with image handling in ASP.NET deployed on a local IIS server?

My web app, built using ASP.NET 4 MVC2 architecture in Visual Studio 2010, has been deployed to my local IIS server under the virtual directory EasyMan4. Recently, I added an image to the Content Folder via Solution Explorer. In the Site.Master page, I ins ...

Unable to use jQuery to send a basic ajax request in order to retrieve a json file

Here is the code snippet: $(function() { $.ajax({ url:'http://localhost:3000/business_places', type:'GET', dataType:'jsonp', done: function(data){ alert(1) }, error: function(data) { ...