Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Best pratice to reduce code redundance on simple jQuery mobile page

Posted on 2013-05-14
2
243 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

791 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