Skip to content

Instantly share code, notes, and snippets.

@getify
Created June 11, 2012 23:23
Show Gist options
  • Select an option

  • Save getify/2913392 to your computer and use it in GitHub Desktop.

Select an option

Save getify/2913392 to your computer and use it in GitHub Desktop.
var $options = $("<option></option>");
$options.add($("<option></option>"));
$options.add($("<option></option>"));
console.log($options.length); // 1 -- wtf?
console.log($options.html()); // <option></option> -- again, wtf?
// the documentation for jQuery#add seems to indicate this should work but it doesn't. Any ideas?
Copy link
Copy Markdown

ghost commented Jun 11, 2012

add doesn't seem to mutate the original collection—it creates a new one:

$("<option></option>").add($("<option></option>")).
  add($("<option></option>")).length; // 3

Edit: Yep. Third paragraph in the docs:

The following will not save the added elements, because the .add() method creates a new set and leaves the original set in pdiv unchanged...

@jrf0110
Copy link
Copy Markdown

jrf0110 commented Jun 11, 2012

$options = $options.add(...)

@getify
Copy link
Copy Markdown
Author

getify commented Jun 11, 2012

thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment