A very cool feature of jQuery is the fact that most of the methods returns a jQuery object that you can then use to call another method. {link}
This also increases performance because you do not have to re-select the objects each time.
I am working a form validation model and I want to copy a field and all its attributes into a dialog box to present to the user and have them correct it “in their face” so to speak.
So I want to:
- Clone the field (generic passed in as JSON string)
- Append it to the contentFieldset
- Rename the field id to make sure there is no conflict
- With the minimum amount of code/effort, while maintaining maximum readability
msg = '{"fieldname": "First_Name", "error": "Enter a First Name"}' var obj = jQuery.parseJSON(msg); $("#"+obj.fieldname).clone().appendTo("#contentFieldset").attr("id", "validate_"+obj.fieldname);
And that created the field along with the text which was already entered
<fieldset id=”contentFieldset”>
<input id=”validate_First_Name” size=”25″ value=”Marky Entered this” name=”DEP_FNAME”>
</fieldset>
Productive !