JQuery Default Form Values Advice

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
JQuery .clone() help

Hi,

A quick JQuery help thread, this is nothing major, but would be nice to have :)

I'm using clone() to append new form fields to a form like this:

Code:
$('#orders').append($('#orders tr:last').clone(true));
This adds another field as expected, but keeps the values from the previous input - I can't seem to get the new inputs blank :( Have tried chaing the .val(" ") function too, with no luck. Any ideas?
 
Last edited:
You could always reset the form after cloning no?
$('#formid').get(0).reset();

Probably won't work if the values you're trying to get rid of are actually in the HTML mind...
 
Resetting the form would clear all values though, which could get annoying if you're half way through the fields when you add a new one.

I think the problem may be because you're duplicating the entire table row, and not acessing the value of the input directly - try this:

Code:
$("INPUT", "#orders tr:last").attr("value", "")
$("SELECT", "#orders tr:last").attr("value", "") // the value here should be the value of your 'Please select...' option
 
For those interested, I just randomly figured this out weeks later :)

I decided to try the slightly less elegant .append:

Code:
$('table#orders').append(<tr><td>content</td></tr>);

Now I tried this before but it didn't work as I had spaces with the html I wanted to append (to make it easier for me to read) E.g.

Code:
$('table#orders').append(<tr>
<td>content</td>
</tr>);

Removing these spaces and appending the html as a single line worked. Now all the inputs within my appended html are blank (what I wanted) and not duplicated from the previous row.


Resetting the form would clear all values though, which could get annoying if you're half way through the fields when you add a new one.

I think the problem may be because you're duplicating the entire table row, and not acessing the value of the input directly - try this:

Code:
$("INPUT", "#orders tr:last").attr("value", "")
$("SELECT", "#orders tr:last").attr("value", "") // the value here should be the value of your 'Please select...' option

Must be the way I have this coded but that doesn't work :(

Off topic, have you got a link to way you've used the JQuery selectors there, as I haven't seen thasty before :)
 
Glad you got it working :)

Here's a link to all the jQuery selectors. The one I used basically says, give me the Input/Select element which is inside the last TR of the element with the ID 'orders'.
 
Back
Top Bottom