admin管理员组文章数量:1334184
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
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
Share Improve this question asked Nov 20, 2012 at 16:29 DomDom 3,12614 gold badges48 silver badges69 bronze badges 2- Do you know how to add/remove a class to an element? – epascarello Commented Nov 20, 2012 at 16:32
- Unfortunately I don't know much about JavaScript. – Dom Commented Nov 20, 2012 at 16:35
3 Answers
Reset to default 2It 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
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>
本文标签: javascriptAdding class to input when default value changedStack Overflow
版权声明:本文标题:javascript - Adding class to input when default value changed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313942a2451434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论