0

I have a form where you have to enter properties of House, like name,number etc. In the same form they will mention for how many number of houses you have to enter these properties.Based on the number of house i'm generating the input fields for each house like this.

   var text1 = '<input type="text" min="1" id="houseNo[' + i + ']" class="Houseno form-control" name="projectConfigurationDetails[' + i + '].houseNo">';

After that when i'm trying read value like this

  $('#houseNo[0]').attr('name');

its giving me undefined After some trail and errors we changed id to "houseNo' + i + '" by removing square brackets.Then

$('#houseNo0').attr('name');

That is giving perfect value.Are square Brackets making Jquery think like that is some selector pattern but we are mentioning # so that is id.

If i'm missing some thing please educate me.

udaybhaskar
  • 159
  • 5
  • 16

2 Answers2

4

As [] is a meta characters, You need to escape meta-characters use \\

$('#houseNo\\[0\\]').attr('name');

Docs

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

Satpal
  • 132,252
  • 13
  • 159
  • 168
2

Consider using underscores (houseNo_3) in the future if you have no interest in using Arrays.

Rick
  • 45
  • 5