最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript : text box with default value - Stack Overflow

programmeradmin2浏览0评论

I have text boxes with default values, I am not able to enter values into them when i am trying to enter, I have to first delete the existing value using delete key then only i can enter.

I want to enter values and on tab change it should change for the other box as well, currently it is changing focus but I cannot enter values into them.

<input type="text" name="namebox" id="{concat('textboxvalue', position())}" value="{@proj}" style="font-size:10px;padding:0px;height:15px;width:90px;text-align:right;"/>

@proj: values ing from database

Thanks, Ani

I have text boxes with default values, I am not able to enter values into them when i am trying to enter, I have to first delete the existing value using delete key then only i can enter.

I want to enter values and on tab change it should change for the other box as well, currently it is changing focus but I cannot enter values into them.

<input type="text" name="namebox" id="{concat('textboxvalue', position())}" value="{@proj}" style="font-size:10px;padding:0px;height:15px;width:90px;text-align:right;"/>

@proj: values ing from database

Thanks, Ani

Share Improve this question edited Jul 25, 2013 at 10:01 George 36.8k9 gold badges69 silver badges109 bronze badges asked Jul 25, 2013 at 9:55 AnimeshAnimesh 411 gold badge3 silver badges8 bronze badges 3
  • <input type="text" name="tproj" id="{concat('txtboxAdjustment', position())}" value="{@proj}" style="font-size:10px;padding:0px;height:15px;width:90px;text-align:right;" /> – Animesh Commented Jul 25, 2013 at 9:56
  • <input type="text" name="tproj" id="{concat('txtboxAdjustment', position())}" value="{@proj}" style="font-size:10px;padding:0px;height:15px;width:90px;text-align:right;" /> @proj - is default value ing from database there are multiple text boxes i am creating in loop – Animesh Commented Jul 25, 2013 at 9:58
  • @Animesh Put it in your question – George Commented Jul 25, 2013 at 9:59
Add a ment  | 

4 Answers 4

Reset to default 5

You can use placeholders in the input tags and textarea tags:

<input type='text' placeholder='Default text here' />
<textarea placeholder='Default text here'></textarea>

Then use fallback with jQuery:

$(document).ready(function(e){
    var defaultVal = "Default text here";
    $("input, textarea").val(defaultVal).on("focus", function(){
        if($(this).val() == defaultVal){
            $(this).val("");
        }
    }).on("blur", function(){
        if($(this).val() == ""){
            $(this).val(defaultVal);
        }
    });

});

Here: http://jsfiddle/EZexQ/1/

I would suggest you to have a look at this css-trick article.
It has an example with html5 placeholder attribute with jquery fallback.

function elementSupportsAttribute(element, attribute) {
  var test = document.createElement(element);
  if (attribute in test) {
    return true;
  } else {
    return false;
  }
};  


if (!elementSupportsAttribute('textarea', 'placeholder')) {
  // Fallback for browsers that don't support HTML5 placeholder attribute
  $("#example-three")
    .data("originalText", $("#example-three").text())
    .css("color", "#999")
    .focus(function() {
        var $el = $(this);
        if (this.value == $el.data("originalText")) {
          this.value = "";
        }
    })
    .blur(function() {
      if (this.value == "") {
          this.value = $(this).data("originalText");
      }
    });
} else {
  // Browser does support HTML5 placeholder attribute, so use it.
  $("#example-three")
    .attr("placeholder", $("#example-three").text())
    .text("");
}

On the focus. Just remove the values. Following is sample code in jquery. Can be done with plain javscript though.

<input value=10>
<input value=20>
<input value=30>

$('input').on('focus', function(){
    $(this).val('')
})

There is a DOM property called defaultValue for input elements. Am giving this solution as the placeholders do not work in older browsers.

First name: <input type="text" name="fname" value="John"><br>
Last name: <input type="text" name="lname" value="Doe"><br>

In your jQuery, it can be accessed by prop('defaultValue') So try this.

$('input').focus(function(){
 if($(this).val() == $(this).prop('defaultValue'))
    $(this).val('');
}).blur(function(){
    if($(this).val() == '')
    $(this).val($(this).prop('defaultValue'));
});

Working fiddle here

发布评论

评论列表(0)

  1. 暂无评论