Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Best pratice to reduce code redundance on simple jQuery mobile page

Posted on 2013-05-14
2
Medium Priority
?
249 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 2000 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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

604 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