Tuesday, October 2, 2012

JQuery get field's default value (defaultValue) HOW-TO

Hi, Readers :)
If you got here, probably you already tried to reach field's defaultValue with jQuery and had no success.
Fine, i will show how to retrieve field's default value.
It is pretty easy to reach it.
I am showing real world example, the field that has "Search phrase..." and on focus have to be cleared, on blur have to return "search phrase..." if field is still blank.
 $('#widget_form_search #search_fld').live('focus', function(){
  if($(this)[0].defaultValue == $(this).val()){
   $(this).val('');
  }
 });
 $('#widget_form_search #search_fld').live('blur', function(){
  if('' == $(this).val()){
   $(this).val($(this)[0].defaultValue);
  }
 });

As you may see, accessing defaultValue is pretty easy
var defVal = $('#field_selector')[0].defaultValue;
Have fun:-)
Sincerely,
Ruskevych Valentin

No comments: