Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong> Another option would be to disable the fields, and enable them when you submit the form:</p> <pre><code>$("input[type='text'][name='ShipCity']").val('Madison').attr('disabled', 'disabled'); $("form").submit(function() { $(':input', this).removeAttr('disabled'); }); </code></pre> <p>I think this should work anyway.</p> <hr> <p><strong>Original answer:</strong></p> <p>This will <code>return false;</code> on <code>keypress</code> events.</p> <p>Also, it stores the default value using jQuery's <code>.data()</code>, and checks that value in a <code>blur</code> event handler.</p> <p>The reason for this is that it is possible to enter a new value without a keypress event (via the GUI menus).</p> <pre><code>$("input[type='text'][name='ShipCity']") .val('Madison') // give default value .data('defaultVal', 'Madison') // remember default value .keypress(function() { return false; // prevent keypress events }) .blur(function() { if(this.value !== $.data(this, 'defaultVal')) // set back to the default this.value = $.data(this, 'defaultVal'); // if it was changed via }); // the GUI menu </code></pre> <hr> <p><strong>EDIT:</strong></p> <p>Here's a version that will let you do it so that you only need to type the default value once. <em>(It is otherwise identical.)</em></p> <p><strong>Example:</strong> <a href="http://jsfiddle.net/cdzuR/" rel="nofollow noreferrer">http://jsfiddle.net/cdzuR/</a></p> <pre><code>$("input[type='text'][name='ShipCity']") .data('defaultVal', 'Madison') // You only need to give the default once .val(function() { return $.data(this, 'defaultVal'); }) .keypress(function() { return false; }) .blur(function() { if(this.value !== $.data(this, 'defaultVal')) this.value = $.data(this, 'defaultVal'); }); </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