Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It works. But is it best practice?

Tags:

jquery

When I start to learn a new language, I always feel like I'm not doing it the practical, standard way. So here's a question regarding jQuery and if what I'm doing is acceptable.

I have 3 images.

<img src="del.png" class="delete user" />
<img src="del.png" class="delete event" />
<img src="del.png" class="delete recipe" />

Then I have jQuery detect a click event on $('.delete').

$('.delete').click(function() {
    if( $(this).hasClass('user') ) {
        action = 'user'
    }

    if( $(this).hasClass('event') ) {
        action = 'event'
    }

   if( $(this).hasClass('recipe') ) {
        action = 'recipe'
    }

   // I then use ajax, send the action as $_POST to the PHP script
   // and it tells PHP what to delete.

   // Doing it this way, I don't have to use 3 onclick functions and 3 ajax functions.


});
like image 354
sqram Avatar asked Nov 29 '22 07:11

sqram


1 Answers

I agree with @Tommi, you are keeping yourself from writing more code than you need, and also consolidating your ajax call to a single place instead of having 3 roughly identical ajax calls (or writing an ajax function and calling it from 3 separate locations).

If this works for you and is not confusing, I don't think there's anything wrong with it. In fact, there are a number of ways to do what you're trying to do in an ugly and inelegant way, but this isn't one of them.

like image 194
Brett Bender Avatar answered Dec 16 '22 13:12

Brett Bender