Will_S
Will_S

Reputation: 71

any array of dropdown

How do you make this work? <select id="sel[]">, which is an array of all the dropdown boxes.But if I change $('#sel').change(function() { to $('#sel[]').change(function() {, it does not work?

Upvotes: 0

Views: 231

Answers (2)

calebds
calebds

Reputation: 26228

The string sel[] is not a valid element id:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Reference

Better to use a simpler id like selArr, for example.


An array of drop-down options will not automatically be created for you. You'll have to maintain the data model yourself, like:

var opts = ['one', 'two', 'three'];

Then add to this array as necessary, and build your drop-down out of it:

This approach keeps the model and view separated, a good programming practice for user interfaces.

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69915

You can escape the square brackets by two back slashes \\.

$(function() {
    $('#sel\\[\\]').change(function() { ... });
});

Demo

But ideally you should not use such conventions for naming ids.

Upvotes: 1

Related Questions