aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes jQuery Custom Function: Improvements? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jQuery Custom Function: Improvements?" Watch "jQuery Custom Function: Improvements?" New topic
Author

jQuery Custom Function: Improvements?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I've been trying something new (to me) lately while developing with jquery. I've been trying to write all my code as plugins or functions to jquery so that they become more reusable out of the box, so to speak. So even if the code is only being used in one place I write it so that it could be generally used elsewhere. I know, I know, everyone should be doing this and not just in JavaScript. Anyway...

I have a UL with dynamically added LI's. User types in some text into a text input and they click the Add button (or hit enter) and I add an LI with the text and a delete image. I wrote this little function so that the user passes in the text and I create the LI and I call a callback function the user specifies for the click() event of the image element.



And here is how it is used:



I'm just looking for some general improvement suggestions. It literally doesn't need to do more than it does right now. So adding functionality isn't really necessary.

Thanks.
[ August 24, 2008: Message edited by: Gregg Bolinger ]

GenRocket - A Test Data Generation Platform
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Originally posted by Gregg Bolinger:
I've been trying something new (to me) lately ...

This is very common in the jQuery community.

The rest looks fine to me, thought there are a bazillion different ways to do it. Personally, I tend to rely less on intermediate variables.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Originally posted by Bear Bibeault:
Personally, I tend to rely less on intermediate variables.


I assume you mean the variable creation for text_span, img_span, etc? If so, I thought about this. I for sure need the img instance so I can create the click event. I tried using less but it became a lot less readable. Do you have suggestions?
[ August 24, 2008: Message edited by: Gregg Bolinger ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

More variables will improve readability.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Thanks. And just for completeness I now have a delete function that looks like this:



Using the add and delete functions, the actual implementation looks like this:



Man, I love jquery.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Originally posted by Gregg Bolinger:
Man, I love jquery.
For me, it's put all the fun back into writing client-side scripts. It's no longer a chore, but something I actually enjoy doing.
Uzbek Jon
Greenhorn

Joined: Dec 02, 2008
Posts: 5
Gregg Bolinger wrote:

I'm just looking for some general improvement suggestions. It literally doesn't need to do more than it does right now. So adding functionality isn't really necessary.

Thanks.
[ August 24, 2008: Message edited by: Gregg Bolinger ]


Hi Gregg (and happy new year )

I came accross this thread through Google. There was another helpfull article on jquery custom functions. It suggests to hide your plugin code and make it more other javascript frameworks friendly. For example:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Uzbek Jon, please read this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jQuery Custom Function: Improvements?
 
Similar Threads
jquery accordion is causing jquery treeview picker to breakup
Menu image positioning
Need Help with Jquery Selector logic
Can you read this?
selecting pure text in jquery