Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
Results 1 to 3 of 3
-
04-08-2016, 04:33 AM #1New to the CF scene
- Join Date
- Apr 2016
- Posts
- 2
- Thanks
- 0
- Thanked 0 Times in 0 Posts
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:
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.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(); });
I would like to be able to write something like this:
instead ofCode:$deleteno.attr("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.Code:$(".btn_deleteno[data-id="+id+"]").show();Last edited by VIPStephan; 04-08-2016 at 07:53 AM. Reason: changed quotes to use code BB tags
-
04-08-2016, 07:57 AM #2The fat guy next door
- 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.
-
04-08-2016, 11:13 PM #3New to the CF scene
- Join Date
- Apr 2016
- Posts
- 2
- Thanks
- 0
- Thanked 0 Times in 0 Posts
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



Reply With Quote
