Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JQuery - Store a button in a variable and call it later by a different parameter?

    NOTE: If you notice a spelling error in the quoted boxes below, it's because Coding Forums is most likely auto-replacing some of these words. (I even tried wrapping the info in [ code ][ /code ], but that didn't work either) So there are probably visual errors below, but keep in mind that what I've written WORKS - and I just need some help cleaning it up.




    I have a declared a few buttons as variables just to make the script look a bit cleaner.

    Example:
    Code:
    var $edit = $(".btn_edit");
    var $delete = $(".btn_delete");

    I also have a function that toggles visibility of the edit/delete buttons as well as confirmations and cancel buttons for each of these options.
    Everything works except I want to clean up the toggle function.
    It currently looks like this:

    Code:
    $delete.on("click", function(){
       var id = $(this).attr("data-id");
       $(this).hide();
       $(".btn_deleteno[data-id="+id+"]").show();
       $(".btn_deleteyes[data-id="+id+"]").show();
    });
    In the above function, if the user clicks on "delete" 2 more buttons appear: confirm and cancel, and the original delete button is temporarily hidden from view.

    I would like to be able to write something like this:
    Code:
    $deleteno.attr("data-id="+id).show();
    instead of
    Code:
    $(".btn_deleteno[data-id="+id+"]").show();
    This way, if I ever need to change any of these buttons, I'll only need to change the declared variables at the top of the script.
    Last edited by VIPStephan; 04-08-2016 at 07:53 AM. Reason: changed quotes to use code BB tags

  2. #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    9,808
    Thanks
    6
    Thanked 1,159 Times in 1,130 Posts
    You can use the filter() function to reduce a collection to specific items. So, you could first store all buttons in one variable, and then use filter to reduce the collection to, say, only “no” buttons. However, it looks like you could do various things much better. But I can’t tell you how because I don’t know the context; you’ll have to show the complete HTML and JS for that.

  3. #3
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by VIPStephan View Post
    You can use the filter() function to reduce a collection to specific items. So, you could first store all buttons in one variable, and then use filter to reduce the collection to, say, only “no” buttons. However, it looks like you could do various things much better. But I can’t tell you how because I don’t know the context; you’ll have to show the complete HTML and JS for that.
    Do you have any recommendations/reading material that might help me clean up my code? I'm really new to JQuery (like I just started using it last week).
    Essentially, I only have one "professional" looking script that I got from a book, but my main.js page primarily looks like:

    variable declarations...

    $button1.on("click", function(){ //do something 1 });
    $button2.on("click", function(){ //do something 2 });
    $button3.on("click", function(){ //do something 3 });

    reusable functions...

    It's organized well enough, but I know it could be better. Every book I read is basically the same crap over and over for absolute beginners. Like writing variables takes up an entire chapter. I need Intermediate and Advanced books or sources that I can learn from


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •