Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

jQuery Custom Function: Improvements?

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More variables will improve readability.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uzbek Jon, please read this.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic