aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes jquery - code review Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jquery - code review" Watch "jquery - code review" New topic
Author

jquery - code review

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

This seems poorly symmetrical. The code to take a string and turn into zero or more selected options in a select list is one line. The code to do the inverse is a story. Am I missing some pattern for doing the later? I originally had it as one line without the null check and got a JavaScript null reference.



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What about that ?


[My Blog]
All roads lead to JavaRanch
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Christophe Verré wrote:What about that ?


.val() returns a string so the join is useless.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
$(...) is expensive, you want to try to minimize the amount of times you look things up.

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Eric Pascarello wrote:.val() returns a string so the join is useless.


http://api.jquery.com/val/

In the case of <select multiple="multiple"> elements, the .val() method returns an array containing each selected option.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61623
    
  67

I'd use chaining and the this pointer in the handler body to minimize calls to $().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
// the one liner gives an error
// $('#a').val($('#b').val().join(',');


That line is missing a )




Running example here: http://jsbin.com/ivubi4/
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Christophe Verré wrote:
Eric Pascarello wrote:.val() returns a string so the join is useless.


http://api.jquery.com/val/

In the case of <select multiple="multiple"> elements, the .val() method returns an array containing each selected option.


Totally missed select in her question...
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

I learned two things in this thread. The idiom of merging things with or (which is very Perl like but it didn't occur to me for JavaScript) and that you can chain an event handler with something that executes now. Both of which make perfect sense now that they came up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jquery - code review