1

I understand using $('input[name="_"']).val('');will replace an input field by name.

I want to use a loop to cycle through a range of names. $('input[name="'variable_name'"']).val(''); results in an error saying the line is missing a closing bracket ')'.

Is there a way to do this?

2
  • 3
    You need to add + for string concatenation $('input[name="' + variable_name + '"']).val(''); Commented May 11, 2016 at 21:20
  • 1
    I can't believe I missed that. Thanks Alon! Commented May 11, 2016 at 21:23

1 Answer 1

1

In javascript you need to add + to concatenate strings, and the jquery selector requires a string. The solution would thus be:

$('input[name="'+variable_name+'"]').val(''); //Note the addition of the 2 + signs

You also misplaced a single quote at the end (before the ] instead of after)

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

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.