7

What I have

I have a text field, the mark-up of which I cannot directly edit:

<input type="text" name="email" value="Email Address" id="subscribe-field" 
onclick="if ( this.value == 'Email Address' ) { this.value = ''; }" 
onblur="if ( this.value == '' ) { this.value = 'Email Address'; }">

What I need

I need the text field value to be blank regardless of whether the text field is clicked, not clicked or clicked out of.

What I've tried

I can disable the value:

$('#subscribe-field').val('');

I've tried to disable the onclick and onblur values:

$('#subscribe-field').click(function() { return false; });
$('#subscribe-field').off('click');
$('#subscribe-field').unbind('click');

...however, the value reappears as soon as I click outside of the text field.

My question

How do I remove the onblur value from the above mark-up?

Dominor Novus
  • 2,248
  • 5
  • 29
  • 35

2 Answers2

11

try this way :

$('#subscribe-field').removeAttr('onclick');

$('#subscribe-field').removeAttr('onblur');

Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59
1

You should remove inline JavaScript. jsfiddle. Using jQuery.one will make sure that focus event will get called only once.

<input type="text" name="email" value="Email Address" id="subscribe-field" />



$('#subscribe-field').one('focus',function() { 
            this.value = ''; 
       return false; 
});

UPDATE: As suggested by mahmoud, you should remove onClick and onBlur attribute if you can't edit your html.

off and unbind will unbind jQuery click event not inline onclick and return false will also would not solve your problem as inline onClick will get called first.

Community
  • 1
  • 1
Anoop
  • 23,044
  • 10
  • 62
  • 76
  • 1
    "I have a text field, the mark-up of which I cannot directly edit" so he can't remove the inline javascript. – Chris Oct 11 '12 at 11:25
  • @Shusl - Thanks but for some reason that code did not prevent the value from reappearing when clicking outside the text field. Do I need to be using additional jQuery code in conjunction with what you've posted? Also, is your method superior in any way to what mahmoud has suggested? – Dominor Novus Oct 11 '12 at 11:25