Question

I have an input field that has a default value added with JavaScript

<input type="text" class="input-text required-entry validate-value-eg" id="city_field" name="city" onfocus="if(this.value=='e.g. Bristol, Yorkshire') this.value='';" onblur="if(this.value=='') this.value='e.g. Bristol, Yorkshire';" value="e.g. Bristol, Yorkshire">

Whenever visitor click on this field the default value will dissipated and restore if not replaced by other value.

What I am trying to achieve is to add a class only if value has been changed from default?

Any help much appreciated,

Thanks

Dom

Was it helpful?

Solution

It is always a better practice to define your events in the script section or a separate .js file.

You don't need to handle that in a .change() event . You can check that in the .blur() event itself..

HTML

    <input type="text" class="input-text required-entry validate-value-eg" 
           id="city_field" name="city" value="e.g. Bristol, Yorkshire">

Pure Javascript

var elem = document.getElementById('city_field');

elem.addEventListener('focus', function() {
    if (this.value == 'e.g. Bristol, Yorkshire') {
        this.value = '';
    }
});

elem.addEventListener('blur', function() {
    if (this.value == 'e.g. Bristol, Yorkshire') {
        RemoveClass(this, "newClass")
    }
    else if (this.value == '') {
        this.value = 'e.g. Bristol, Yorkshire';
        RemoveClass(this, "newClass")
    }
    else {
        this.className += " newClass";
    }
});

function RemoveClass(elem, newClass) {
    elem.className = elem.className.replace(/(?:^|\s)newClass(?!\S)/g, '')
}​

Javascript Fiddle

But you can achieve this with a loss lesser code by using other javascript frameworks. This will make your life a lot easier but it is always good if you start out with javascript.

jQuery

$(function() {
    var $elem = $('#city_field');

    $elem.val('e.g.Bristol, Yorkshire'); // Default value
    $elem.on('focus', function() { // Focus event
        if (this.value == 'e.g.Bristol, Yorkshire') {
            this.value = '';
        }
    });
    $elem.on('blur', function() { // Focus event
        if (this.value == 'e.g.Bristol, Yorkshire') {
            $(this).removeClass("newClass");
        }
        else if (this.value == '') {
            this.value = 'e.g.Bristol, Yorkshire';
            $(this).removeClass("newClass");
        }
        else {
            $(this).addClass("newClass");
        }
    });
});​

jQuery Fiddle

OTHER TIPS

Add an onchange handler to your input

<input type="text" class="input-text required-entry validate-value-eg" 
    id="city_field" name="city" 
    onchange="addClass(this)" 
    onfocus="if(this.value=='e.g. Bristol, Yorkshire') this.value='';" 
    onblur="if(this.value=='') this.value='e.g. Bristol, Yorkshire';" 
    value="e.g. Bristol, Yorkshire">


<script>
 function addClass(input) // 'this' in onChange argument is the input element
 {
     if( input.value=='e.g. Bristol, Yorkshire')
     {
        $(input).removeClass("not_default_input_class");
        $(input).addClass("default_input_class");
     }
     else 
     {
        $(input).removeClass("default_input_class");
        $(input).addClass("not_default_input_class");
     }
 }
</script>

EDIT: Added use of jQuery to add/remove the CSS classes

 <script>
 function addClass(input) // 'this' in onChange argument is the input element
{
 input.className = input.value==input.defaultValue? 
     "default_input_class" : "not_default_input_class"
}
</script>   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top