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

javascript - How to remove focus from textbox? - Stack Overflow

programmeradmin4浏览0评论

When opening form the cursor is waiting in the text box along with help text.So there are some validation error happening. So i don't want focus when opening form, user have to click the text and type the text. But here didn't any onfocus eventhough cursor in the textbox .

if(!$.trim($("#productsTextArea1").val())){
        helpTip = "help string ....";
        $("#productsTextArea1").val(helpTip);
        $("#productsTextArea1").css("color","#999");
        $("#productsTextArea1").click(function(event){
            if($("#productsTextArea1").val().indexOf("Enter a return")>-1){
                $("#productsTextArea1").css("color","black").val("");
            }
        });
    } else {
         $("#productsTextArea1").blur();
    }

Please advise...

JSFIDDLE

When opening form the cursor is waiting in the text box along with help text.So there are some validation error happening. So i don't want focus when opening form, user have to click the text and type the text. But here didn't any onfocus eventhough cursor in the textbox .

if(!$.trim($("#productsTextArea1").val())){
        helpTip = "help string ....";
        $("#productsTextArea1").val(helpTip);
        $("#productsTextArea1").css("color","#999");
        $("#productsTextArea1").click(function(event){
            if($("#productsTextArea1").val().indexOf("Enter a return")>-1){
                $("#productsTextArea1").css("color","black").val("");
            }
        });
    } else {
         $("#productsTextArea1").blur();
    }

Please advise...

JSFIDDLE

Share Improve this question edited Jul 16, 2013 at 10:46 user2444474 asked Jul 15, 2013 at 18:22 user2444474user2444474 5738 gold badges21 silver badges41 bronze badges 1
  • Why are you running this function on page load? Why not defer it until the user actually clicks on the textarea? – Diodeus - James MacFarlane Commented Jul 15, 2013 at 18:28
Add a ment  | 

1 Answer 1

Reset to default 11

UPDATE:

Use HTML5 Placeholder attribute, you don't need to use any js.

    <textarea id="productsTextArea1" name="product" rows="5" 
placeholder="Some Hint Text is Placed Here"></textarea>

To disable autofocus on all input elements use this.

  $(function(){
        $('input').blur();
    });

To disbale autofocus on a particular element, pass the element id as below:

$(function(){
    $('input#someid').blur();
});
发布评论

评论列表(0)

  1. 暂无评论