0

I want to know if its possible writing some code which will delete an initial value from a form when the user clicks on the corresponding form (described in the following example)?

class example(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    descr = forms.CharField(initial='Please insert a relevant description ...')

I dont want to use the help_text attribute.

PS: For a better understanding I could make an analogy with java onclick event

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
marlboro
  • 254
  • 6
  • 16

3 Answers3

0

Because it's a webservice you'll have to use javascript. If you use jQuery, something like this:

$(document).ready(function(){
    $('#id_descr').one('click', function(){
        $(this).val('');
    });
});
Will
  • 4,498
  • 1
  • 21
  • 20
0

for the backend part, have a look at this answer. For the frontend, you may wish to add a html5 shiv to get placeholders in legacy browsers

Community
  • 1
  • 1
second
  • 28,029
  • 7
  • 75
  • 76
0

One way is to use HTML5's placeholder attribute directly from Django:

descr = forms.CharField(widget=forms.TextInput(attrs={
    'placeholder': 'Please insert a relevant description ...'}))

The unique constraint is that it is not yet supported by Internet Explorer.

caio
  • 1,949
  • 16
  • 20