• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jquery - code review

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about that ?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:What about that ?


.val() returns a string so the join is useless.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$(...) is expensive, you want to try to minimize the amount of times you look things up.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use chaining and the this pointer in the handler body to minimize calls to $().
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic