开发者

Display cursor and text both in textbox when page load

开发者 https://www.devze.com 2023-02-20 09:59 出处:网络
I am working on Asp.net MVC. i am using regular html <label for=\"txtbox1\" > User Name</label>

I am working on Asp.net MVC.

i am using regular html

  <label for="txtbox1" > User Name</label>
<input type="text" id="txtbox1" name="text" class="water"/>

I want to display Cursor before text and Text both when page load. When user start writting in text bo开发者_JAVA百科x at that time "User Name" removed.


You need to use javascript for this. If you are using jquery you could:

$(function() {
    $('#txtbox1').focus();
});

And if you want to do this with plain javascript:

window.onload = function() {
    var textbox = document.getElementById('txtbox1');
    if (textbox != null) {
        textbox.focus();
    }
};

UPDATE:

If you want the text to disappear when the user starts typing you may try the following:

$('#txtbox1').keypress(function() {
    if (!$(this).data('dirty')) {
        $(this).val('').data('dirty', true);
    }
});

Live demo.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号