Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove span outside of div on input due to Rails 3 `field_with_errors`
    text
    copied!<p>In my Rails 3 app I remove a <code>&lt;span&gt;</code> that I use as a placeholder on input with jQuery. If I submit the form and I have errors, the form reloads and the errors are shown. However, for the fields where there were errors, the <code>&lt;span&gt;</code> won't clear on input because the <code>&lt;span&gt;</code> is no longer within the <code>&lt;div&gt;</code> containing the input. (This is due to the field_with_errors <code>&lt;div&gt;</code> added from the error.) How can I work around this and still remove the <code>&lt;span&gt;</code> despite field_with_errors?</p> <p><strong>Here's my jQuery:</strong></p> <pre><code>// placeholder label for text $(function() { $("span.holder + input").keyup(function() { if($(this).val().length) { $(this).prev('span.holder').hide(); } else { $(this).prev('span.holder').show(); } }); $("span.holder").click(function() { $(this).next().focus(); }); }); </code></pre> <p><strong>My html with field_with_errors (jQuery doesn't work):</strong></p> <pre><code>&lt;div class="holds email"&gt; &lt;span class="holder"&gt;Email&lt;/span&gt; &lt;div class="field_with_errors"&gt; &lt;input autocomplete="off" id="user_email" name="user[email]" size="38" type="text" value=""&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p></p> <p><strong>My html without field_with_errors (jQuery works):</strong></p> <pre><code>&lt;div class="holdsname"&gt; &lt;span class="holder"&gt;Last name&lt;/span&gt; &lt;input autocomplete="off" class="inner" id="user_profile_attributes_last_name" name="user[profile_attributes][last_name]" size="18" type="text"&gt; &lt;/div&gt; </code></pre>
 

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