Solved

Best pratice to reduce code redundance on simple jQuery mobile page

Posted on 2013-05-14
2
238 Views
Last Modified: 2013-05-15
Hi all!

I have a simple jQuery Mobile page where I have 12 buttons.
Each button is a month of the year (Jan, Feb etc etc).

I want that if the user taps on a month a record is created in a mysql database for the month pressed.

If the user press the same month button again the record is deleted.

Right now (being a beginner in his learning phase) I'd do it like:

$('#Jan').click(function(){
        dbChangeGen();
        });

$('#Feb').click(function(){
        dbChangeFeb();
        });

And each function will do its ajax post to php that'll insert or delete the data.

Is there any way not to write 12 almost identical functions?

I don't need specific code (even if a snippet to clarify is always welcome :) ) but I'd need to understand how a real programmer would handle a similar task to improve and learn better.

Thanks!
0
Comment
Question by:ltpitt
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39166659
0
 
LVL 1

Author Closing Comment

by:ltpitt
ID: 39167220
Simple and very clean!

For the sake of future users I paste his solution here:

    var j = {"Jan":"Gen", "Feb":"Feb"}
    $(document).ready(function() {
        $('.month').click(function(ev){
             var month = $(this).attr("id");
             window["dbChange" + j[month] ]();
        });
    })



function dbChangeGen() { alert("January"); }
function dbChangeFeb() { alert("February"); }
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now