Igor's Blog

Creating multiple forms when you want to add or edit data in a web app can be an annoying and an error prone task so why not reuse the same form with a couple of CSS style classes and a bit of jQuery code. Here's how I do it on my Travel Blog project.

To show this idea visually, below is the same form that is used to both enter a new travel destination as well as edit an existing one. Of course this is possible to do with more server-side processing, but I wanted to offload UI tasks such as these to the client side.

addeditform1.png addeditform2.png


My approach was to make an 'add edit' form that could be changed on the fly depending on requirements. This form is placed in its own div and is 'popped up' whenever a specific button is clicked in the web page. I had two main parts of the form that required to be visually different, the heading and the submit button.

The header was defined like this...both Add and Edit spans one after the other...
 HTML
<div class="tb_popup_header">
<span id="tb_loc_pop_add_1">Add</span><span id="tb_loc_pop_edit_1">Edit</span><span> Location
</div>


Similarly my submit button used the same approach...
 HTML
<button type="submit" class="btn btn-success"><span id="tb_loc_pop_add_2">Add</span><span id="tb_loc_pop_edit_2">Edit</span> Location</button>


That is nothing special so far, but the important thing is in the naming of ID attributes. All the 'add' relevant spans start with 'tb_loc_pop_add' and all the 'edit' relevant spans start with 'tb_loc_pop_edit'. Those names are used further down in the jQuery code.



Before getting into the jQuery, there is some CSS that's required. I created two classes that control whether the spans will be displayed or not. It's important to use 'display' instead of 'visible' so that the span we don't want to see is not in the DOM at all.
 CSS
.tb_visible {
display: initial;
}
.tb_hidden {
display: none;
}


Now lets look at the jQuery. I use a variable 'do_add' to determine whether I should show the 'Add' or 'Edit' version of the form. In my case the expression for this is very project specific so I won't show the code for that, however it is just a boolean value that's when set to true will show the 'Add' form and if it's false the 'Edit' form is shown.

This code looks for all the matching elements by their ID, matching the first part e.g. 'tb_loc_pop_add' and 'tb_loc_pop_edit' in my case. Then the hidden and visible classes are toggled on the matching elements.
 jQuery
var do_add = <some boolean expression>
$('[id^=tb_loc_pop_add]').toggleClass('tb_hidden', !do_add);
$('[id^=tb_loc_pop_add]').toggleClass('tb_visible', do_add);
$('[id^=tb_loc_pop_edit]').toggleClass('tb_hidden', do_add);
$('[id^=tb_loc_pop_edit]').toggleClass('tb_visible', !do_add);


That's basically it. I've left out any other form processing that would have to be done like setting input value fields, etc.

-i

comments powered by Disqus
Other posts you may like...
Programming, DIY, Games, Hacks, and Tech

Recent Blog Posts

Converting a CSV file containing commas in quotes to a pipe separated file using Awk and Bash

How to get Facybox gallery popup to work for images with dynamic URLs

jQuery UI Photoboxr plugin updated to version 0.2

Learn How to Add Disclaimer to All Outgoing Emails in Office 365

How to securely host your web site for free with Google Cloud and CloudFlare (part 3)

How to securely host your web site for free with Google Cloud and CloudFlare (part 2)

How to securely host your web site for free with Google Cloud and CloudFlare (part 1)

Beware of the subtle difference in date format strings between MySQL and SQLite

Postman reporting Unexpected 'N' error in response body

Adding extra shelves to the Ikea Detolf display cabinet

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Please follow me for more great content!
     
Don't show this again