MichaelK
MichaelK

Reputation: 31

Brackets in Attribute not A valid Selector

I have an attribute "email[0]" on every Input in my form. The following selector is not working and I get an error saying:

...input[name=email[0]]' is not a valid selector.

var email = "emails[0]";       
var radioInputs = document.querySelectorAll('.input_radio input[name='+name+']');

If I try

var radioInputs = document.querySelectorAll('.input_radio input[name="emails[0]"]');

it works perfectly fine. But how can I put this variable name into my selector without having an error?

Upvotes: 0

Views: 866

Answers (1)

Anurag Srivastava
Anurag Srivastava

Reputation: 14413

You would use:

var name = "emails[0]";
document.querySelectorAll('.input_radio input[name="'+name+'"]');

Or by using template literals:

document.querySelectorAll(`.input_radio input[name="${name}"]`);

Upvotes: 1

Related Questions