| Author |
jquery - code review
|
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26192
|
|
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: 14672
|
|
What about that ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
Christophe Verré wrote:What about that ?
.val() returns a string so the join is useless.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
$(...) is expensive, you want to try to minimize the amount of times you look things up.
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
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: 56201
|
|
|
I'd use chaining and the this pointer in the handler body to minimize calls to $().
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
// 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: 15357
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26192
|
|
|
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.
|
 |
 |
|
|
subject: jquery - code review
|
|
|