Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing placeholder="" for opera through jquery
    primarykey
    data
    text
    <p>I have implemented through jQuery the <a href="http://diveintohtml5.info/forms.html#placeholder" rel="nofollow noreferrer">placeholder HTML 5 attribute</a> for the browsers that don't support it (all except webkit at this time).</p> <p>It works really great but it has a small problem: it breaks the HTML 5 <code>required="required"</code> and <code>pattern="pattern"</code> attributes on Opera (it's the only browser that supports them currently).</p> <p>This is because the placeholder value is temporarily set as the input value, and thus Opera thinks on form submission that the input is actually filled with the placeholder value. So I decided to remove the placeholders when the form is submitted:</p> <pre><code>$('form').submit(function() { $(this).find(".placeholder").each(function() { $(this).removeClass('placeholder'); $(this).val(''); }); }); </code></pre> <p>This worked but another problem arose: if the form client-side validation failed (because of the <code>required</code> or <code>pattern</code> attributes) then the fields aren't re-given their placeholder value.</p> <p>So, <strong>is there a way (js event?) to know if/when the form submission failed client-side</strong>, so I can re-add the placeholders?</p> <hr> <p><strong>Test case</strong>: open <a href="http://koper.wowpanda.net/test.html" rel="nofollow noreferrer">this</a> with a browser that supports required/pattern but not placeholder (only Opera at this time). Try to submit the form without filling any of the inputs; you'll see that when you do the second input loses the placeholder. I don't want it to happen.</p> <hr> <p>This is the complete code, but it's probably not needed:</p> <pre><code>function SupportsPlaceholder() { var i = document.createElement('input'); return 'placeholder' in i; } $(document).ready(function() { if (SupportsPlaceholder()) return; $('input[placeholder]').focus(function() { if ($(this).hasClass('placeholder')) { if ($(this).val() == $(this).attr('placeholder')) $(this).val(''); $(this).removeClass('placeholder'); } }); $('input[placeholder]').keypress(function() { if ($(this).hasClass('placeholder')) { if ($(this).val() == $(this).attr('placeholder')) $(this).val(''); $(this).removeClass('placeholder'); } }); $('input[placeholder]').blur(function() { if ($(this).val() != '') return; $(this).addClass('placeholder'); $(this).val($(this).attr('placeholder')); }); $('input[placeholder]').each(function() { if ($(this).val() != '' &amp;&amp; $(this).val() != $(this).attr('placeholder')) return; $(this).val($(this).attr('placeholder')).addClass('placeholder'); }); $('form').submit(function() { $(this).find(".placeholder").each(function() { $(this).removeClass('placeholder'); $(this).val(''); }); }); }); </code></pre>
    singulars
    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