0

I'm using the following jQuery to clone a set of input fields (1 Select field, 2 Text fields and 1 Number field)

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text']").val("");
});

Is there any way I can extend this so the cloned number fields don't also contain the values.

I've tried

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text|number']").val("");
});

Which doesn't work.

2 Answers 2

6

The selector in .find("input[type='text|number']") is incorrect. To select multiple elements, separate the selectors by comma.

.find("input[type='text'], input[type='number']")

See Multiple Selectors


To set empty string as value to all the input elements, use element selector.

.find('input').val('')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I guessed the | was an invalid seperator, just didn't occur to me to use two selectors with a , seperator
0

You must use a multiple element selector using , to separate each selector; like:

$("#add_button").click(function(e) {
    e.preventDefault();
    $(".form-row :first").clone().insertAfter(".form-row :last").find("input[type='text'], input[type='number']").val("");
});

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.