Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does jquery to add input fields stops working when I add $.ajax for form processing?
    primarykey
    data
    text
    <p>I'm working on a bit of code that asks a user for text input. Additionally, the user has the option to click "Click to add" to create an additional text input field. I was able to get this functionality to work using the jquery below. However, when I tried to add more jquery to process the user input with PHP (this begins with the setting of var dataString in the .js file below), I found that the initial "click to add" functionality stopped working. </p> <p>I'm pretty stumped about why and any insight one could provide would be great. </p> <p>This is my HTML: </p> <pre><code>&lt;div id="form_inputs"&gt; &lt;a href="#" id="add"&gt;click to add&lt;/a&gt; &lt;form method="POST"&gt; &lt;div id="input"&gt; &lt;p&gt;&lt;input type="text" name="user_response[]" id="u_response" /&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; &lt;button id="submit"&gt;submit answers&lt;/button&gt; &lt;/div&gt; </code></pre> <p> This is my current js file:</p> <pre><code>$(function() { var count = $('#form_inputs p').length + 1; $('#add').live('click', function(){ $('&lt;p&gt;&lt;input type="text" name="user_response[]" id="u_response" /&gt;&lt;a href="#" id="remove"&gt;remove&lt;/a&gt;&lt;/p&gt;').appendTo('#input'); count++; return false; }); $('#remove').live('click', function(){ if(count &gt; 2) { $(this).parents('p').remove(); count--; return false; } }); var dataString = $('input#user_response').val(); $.ajax ({ type: "POST"; url: "test3.php"; data: dataString; success: function(){ $('#form_inputs').html('&lt;div id="message"&gt;&lt;/div&gt;'); $('#message').html('&lt;h4&gt;thanks for submitting&lt;/h4&gt;'); } }) }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload