1

So here is this element:

<TD><INPUT type="text" name="when"/></TD>

I can call a function by

  $(document).ready(function() {
    $('input[name=when]').datepicker();
  });

This works fine. (When I click the element, a datepicker shows up).

But what if this is the element?

<TD><INPUT type="text" name="when[]"/></TD>

Then this does not work:

  $(document).ready(function() {
    $('input[name=when[]]').datepicker();
  });

EDIT:

So thank you for answering me guys, I did not know this. However. I let the users add rows to the table dynamically so each added row should inherit this function. Unfortunately it does not work in the dynamically created rows:

    function setClass() {
            $('input[name="when[]"]').datepicker();
//or $('input[name=when\\[\\]]').datepicker();
        }
erdomester
  • 11,789
  • 32
  • 132
  • 234

1 Answers1

2

Use quotes:

$('input[name="when[]"]').datepicker();

In general, when an attribute value has anything in it but A-Z, put it in quotes.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Suggestion is to use quotes always not to confuse any time.. – Bhojendra Rauniyar Sep 08 '14 at 16:50
  • Hi @T.J. Crowder. Although this answers the question it does not always work. When I dynamically add rows to the table, then the datepicker does not show up, only on the first row which is created static. `function setClass() { $('input[name="when[]"]').datepicker(); }` – erdomester Sep 08 '14 at 16:59
  • @erdomester That's another problem? Ask different question. – Bhojendra Rauniyar Sep 08 '14 at 17:00
  • I don't think that's exactly a different a question. If I asked that in a new post, then I would create almost another duplicate (just what I did) – erdomester Sep 08 '14 at 17:09
  • Well it turned out to be a completely different question: http://stackoverflow.com/questions/25733530/unable-to-reference-to-dynamically-created-element-by-name#25733868 – erdomester Sep 08 '14 at 22:21