Created
August 17, 2011 21:00
-
-
Save kellysutton/1152617 to your computer and use it in GitHub Desktop.
Use jQuery for element construction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createMyWrappedInput = function (defaultValue) { | |
var wrapper = $("<div class='Wrapper'></div>"), | |
input = $('<input type="text" />'); | |
input.val(defaultValue); | |
wrapper.append(input); | |
return wrapper; | |
}; |
Never even knew you could do that. I'll play around with it and possibly update the gist. Thanks for the suggestion!
Sure thing!
Here is the documentation on it (it second signature "jQuery( html, props )"):
http://api.jquery.com/jQuery/#jQuery2
…On Fri, Aug 26, 2011 at 12:47 PM, kellysutton ***@***.*** wrote:
Never even knew you could do that. I'll play around with it and possibly update the gist. Thanks for the suggestion!
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1152617
Changing your variable names from wrapper
and input
to $wrapper
and $input
will let you know the variable contains a jQuery object. Especially useful in larger functions/more complicated code.
One other note: If you're finding yourself using lots of concatenation or appending, try using jQuery-tmpl which allows you to create HTML templates that you can populate with data (similar to Django templates).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why not do:
to make it even more "jQuery-ish", increase readability and avoid potentially issues with quotes.