Updating form appearance with jQuery's $.ajax when submitting forms

This is an example of the list of forms on my page: http://jsfiddle.net/GSC3x/

While it functions well, having a lot of list items on the page can make it less user-friendly. I am looking to change the way forms are displayed after clicking on one of the list items.

I want it so that when an item is clicked, the form appears in the center of the page and the other list items become invisible while the form is active. Clicking on the previously selected item again should return it to the normal list view. How can this be achieved?

@@UPDATE - I have added a hyper-professional image to illustrate how it should work! : ]

Answer №1

Check out the code snippet below...

Here is the CSS:

ul{
    position:relative;
}

.centerDiv{
    position:absolute;
    z-index:1000;
    top:0px;
    left:30px;
}​

And here is the Javascript:

$(document).ready(function(){

    $(".slidingDiv").hide();
    $(".show_hide").show();

    $('.show_hide').click(function(e){
       if( $(e.target).next(".slidingDiv").css('display') != 'block'){
            $(".slidingDiv").hide();
            $(".show_hide").hide();
            $(e.target).show();
            $(e.target).next(".slidingDiv").addClass("centerDiv");
            $(e.target).next(".slidingDiv").slideToggle();
       }else{
              $(".slidingDiv").hide();
                $(".show_hide").show();
       }   
    });
});

Answer №2

$(function(){

        $(".slidingDiv").hide();
        $(".show_hide").show();

        $('.show_hide').click(function(event){
             $('.show_hide').not($(this)).next(".slidingDiv").hide();
             $(event.target).next(".slidingDiv").slideToggle();
        });
});

Answer №3

To achieve a smoother animation effect for centering the form, you can use the following code snippet instead of

$(e.target).next(".slidingDiv").slideToggle();
:

var verticalPosition = ($(window).height() - $(this).next(".slidingDiv").height()) / 2 + $(window).scrollTop();
var horizontalPosition = ($(window).width() - $(this).next(".slidingDiv").width()) / 2 + $(window).scrollLeft();         
$(e.target).next(".slidingDiv").css('position', 'absolute');
$(e.target).next(".slidingDiv").animate({
    left: horizontalPosition,
    top: verticalPosition,
}, 500).show();
});

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

Guide on aligning a series of divs in a single row within a parent div

Within the confines of a 400px wide div called "container", there are six left-floated "box" divs, each 100px wide. The combined width of the "box" divs exceeds 400px, resulting in the divs wrapping onto two lines, with 4 on the first and 2 on the second ...

Using Node.js and Express to make an AJAX POST request

I am currently attempting to execute an Ajax call using a JavaScript function within a Node.js/Express application. The code for the function is as follows: function Save_User_Changes(user_id) { alert('saving changes') let data = {} ...

Learn how to toggle the visibility of a gif image with a button click in an ASP.NET application

I am working on an asp page that includes a button. When the button is clicked, I need to display a gif image. Once the process is complete, the image should be hidden again. Here is the code behind: <head runat="server"> <title>Untitled ...

Django is experiencing difficulties loading CSS on certain template files

Working on my page design using twitter-bootstrap, I've created a base.html file that serves as the global base for all templates to extend. However, I'm facing an issue where upon extending the base.html in all templates, the CSS doesn't d ...

The browser is struggling to interpret the incorrect HTML content

I am looking for a way to create a function that can retrieve account data from my database and organize it in a user-friendly HTML table where users can make selections and modifications. The code I have so far is as follows: $.ajax({ url: "./dat ...

ajax rendering in Ruby on Rails

After creating a Recipient, I want to display a success confirmation message but I keep encountering a missing template error: ActionView::MissingTemplate - Missing partial recipients/_recipient with {:locale=>[:fr], :formats=>[:js, :html], :v ...

Restrict the amount of pages displayed in pagination

Currently, I am developing a jQuery pagination tool. While the pagination functionality is working smoothly, I have identified a drawback: On desktop devices, showcasing more than 14 pagination links in a row is acceptable. However, displaying such an ext ...

Error encountered: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, anticipating either a comma or a semicolon

I encountered this error after making changes to the source of my image. Prior to editing, everything was functioning correctly. Below is my complete code: <?php session_start(); require_once("/home/a9440778/public_html/registration/connect.php"); fu ...

What is the most dependable method for referencing a CSS class through programming?

Within my Sharepoint project, I am dynamically generating controls in the overridden CreateChildControls() method. I am uncertain which approach is more preferable when it comes to referencing the .css file: protected override void CreateChildControls() { ...

Troubleshooting: Why is my CSS Div tag failing to

Can someone help me figure out why the CSS isn't working when I try to use a div tag to style a page that I include with include 'login.php'; inside the <div> tag? My main goal is to overlay the form on the page, but for now I just wan ...

The fallback mechanism for the success method in AJAX is ineffective

When the action page returns type x in PHP, success should happen; otherwise, something else happens. $.ajax({ url: "user.php?mysts=chck", type: "POST", data: $("#cntstngfrm").serialize(), succ ...

Ways to create a smooth transition in element height without a known fixed target height

Is it possible to create a smooth height transition for an element when the target height is unknown? Replacing height: unset with height: 100px allows the animation to work, but this presents a problem as the height needs to be based on content and may v ...

extracting URL parameters using AJAX

I have successfully created an AJAX request using the GET method, passing variables to the URL and receiving a response. Now, I need to retrieve the current URL variables by clicking on a newly created button in order to proceed with further processing. Th ...

What is the best way to add CSS to email addresses that are hidden by the WordPress antispambot feature?

My current method involves utilizing the WordPress antispambot code from their Code Reference: https://developer.wordpress.org/reference/functions/antispambot/ While the code is functional, I am interested in customizing the appearance of the email addre ...

Pictures of Model in HTML Django 1.10

Just getting started with Django 1.10, so please bear with me. I'm working on visualizing images from my Clothes model to create a small webshop for practice. Here is my Model: from __future__ import unicode_literals from django.db import models fr ...

Any idea why the HTML Select dropdown isn't functioning properly in Angular 2?

I am facing an issue with an Angular 2 Form. I am trying to include an html select but it is not working. I have checked the Angular 2 Documentation and even the live examples provided, like the HERO FORM, are not working. You can view the Hero Form Live E ...

HTML5 pattern grouping feature is not functioning as expected

I am attempting to validate this regular expression pattern="([a-zA-Z]+[0-9]*){4,}", which essentially means: It must always start with an alphabetic character. If a number is included, there must be at least 4 characters in total; for example, aaaa would ...

When using Cordova on iOS, the AJAX call encounters issues, displaying a message like this: `{"readyState":"0", "responseText":"", "status":0,"statusText":"

After updating Cordova and adding the iOS platform (cordova-ios@^6.1.0) yesterday, I encountered an issue while trying to make an AJAX call via the XCODE simulator. The output showed: {"readyState":"0", "responseText":"&q ...

Guide on incorporating a "choose file" button to store an image in a mongodb database for a meteor application

I recently created a form for my project which includes typical text inputs like "First Name", "Last Name", and "Address". However, I am currently facing a challenge due to my limited knowledge in Meteor, as well as HTML and Javascript. Despite searching e ...

Positioning a secondary div beside a primary div that is centered on the page

As I work on my website, I have a container that encompasses the entire design and is perfectly centered using margin:auto. Now, I am looking to float specific content to the right of this central container in a way that it stays attached to the side reg ...