Solved

Best pratice to reduce code redundance on simple jQuery mobile page

Posted on 2013-05-14
2
246 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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…

726 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