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

, ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
StrFunc() Online
300 George St Blogumentary
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

Where to find the last item (Robbie's lost lyrics) in GravityFalls Lake level

Detecting print requests to print contents of a DIV with JavaScript

Using Google DFP with AdSense on responsive pages

How to change the email subject line in Gmail

Force PHP GD library to load JPEG images that have invalid data

How to uninstall Samsung Smart Switch from your Mac

Facebook adds weather forecasts but forgets to adjust them to your time zone

No sound or microphone when answering calls on an iPhone

Replacing the standard Odroid XU4 fan and heatsink

PHP PDO doesn't work binding multiple IN statement values

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

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again