0

I have a long form with many buttons and inputs inside it. here is an example:

<form>
    <p>#</p>
    <input type="button"  value="Inputs" />
        <input type="text" value="" />
</form>

I want I do not want to clone it because the amount of input fields will be expanded and altered. I am thinking of using the '.after()' function but there are alot of excape strings i have to do.

Is there a way to just call another function and have this returned for legibility sake?

1
  • "Is there a way to just call another function and have this returned for legibility sake?" ...have what returned, in response to what? Commented Feb 24, 2012 at 22:32

2 Answers 2

1

I'm a touch perplexed why you want to avoid clone. if you build the object to be cloned outside of the form and inside a div with style="display:none", and then clone when necessary and move the cloned object into the form with something like insertafter(), you shouldn't see any interference with anything else you're doing.

Sign up to request clarification or add additional context in comments.

4 Comments

i have a function that adds input fields and alters their ID's based on the contents, it was getting really complicated. because i was having to alter 5+attributes because the form is much more complex than the example.
In that case, you can build it as a function - have a default block, clone it, populate the id attribute (and any other attribute) of the clone based on inputs, and then slot it into place. In any case, avoiding cloning isn't going to simplify that for you. Indeed, if you use the clone technique, it lets you handle the attributes one at a time, which can be pretty helpful for code readability and maintainability.
Its not fully featured yet but its here: "jsfiddle.net/rayd360/e9f5q " Im still new to JQuery and using tutorials to guide me but i should have a (document).ready function and clone it then somehow make that clone public to other functions?
No, no - simpler than that. You make a div somewhere out of the way on your page (not inside of any forms) that contains the base HTML that you're looking for, has a unique id, and is set to style="display:none". Then, any time you need a copy of that block, you find the block by id, clone it, make any necessary changes, and insert it. Make a new clone each time.
0

I'm not 100% sure of your question but you can create an element several ways, here are a couple:

$('<input />', { id : 'some-id', type : 'text', value : 'some value' }).appendTo('form');

OR

$('<input id="some-id" type="text" value="some value" />').appendTo('form');

You can also select the form element and append to it like this:

var $newElement = $('<input />', { id : 'some-id', type : 'text', value : 'some value' });
$('form').append($newelement);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.