-2

I'm repurposing some code to dynamically add form fields. So I had this:

<input name="projDesc" />

and used the following to create additional fields:

function GetHtml() {

    var len = $('.extraProject').length;
    var $html = $('.extraProjectTemplate').clone();

    $html.find('[name=projDesc]')[0].name="projDesc" + len;

    return $html.html();    

}

But now I need to switch the input to an array for PHP to process it:

<input name="projDesc[]" />

How do I change the .find method to accommodate this now? Thx.

Followup:

Sorry for the dupe but what are some possible reasons it works in this Fiddle but not on my site? On my site now, I don't even get the initial form field to display.

Also do I need to escape the square brackets in both instances where I mention projDesc?

$html.find('[name=projDesc\\[\\]]')[0].name="projDesc\\[\\]" + len;
BVGESP
  • 1
  • 2

1 Answers1

0

Use this in the selector:

...
$html.find('[name=projDesc\\[\\]]')[0].name = 'projDesc' + len;
...

Answer lies here: jQuery selector for inputs with square brackets in the name attribute

Community
  • 1
  • 1
JC Hernández
  • 777
  • 3
  • 13