开发者

when adding focus to a textbox then adding a space &nbsp using javascript?

$("#inputField").focus()

when the cursor focuses on the texbox i want to d开发者_高级运维o one space, so it moves to the right a little further!! if you know what i mean thanks

function insertParamIntoField(anchor, param, field) {
       var query = anchor.search.substring(1, anchor.search.length).split('&');

       for(var i = 0, kv; i < query.length; i++) {
          kv = query[i].split('=', 2);
          if (kv[0] == param) {
             field.value = kv[1];
             return;
          }
       }
    }


$(function () {
    $("a.reply").click(function (e) {
       console.log("clicked");
       insertParamIntoField(this, "replyto", $("#inputField")[0]);

     $("#inputField").focus()







       e.preventDefault();
       return false; // prevent default action
    });
});


Attach an event handler for the focus event that sets a space if the input is empty:

$("#inputField").focus(function() {
    if($(this).val() == "") {
        $(this).val(' ');
    }
});

Or do you want to add a space every time the box is focused? Then the body of the callback function just needs to be:

$(this).val($(this).val() + ' ');

Update:

Ah I see, well the easiest way would be (forget about attaching an event handler for focus):

$("a.reply").click(function (e) {
       console.log("clicked");
       insertParamIntoField(this, "replyto", $("#inputField")[0]);

       $("#inputField").val($("#inputField").val() + ' ');

       //...
}

But you should make sure that clicking the link twice does not result in adding the name twice!


You can replace this line:

$("#inputField").focus();

With this

$("#inputField").val(" ").focus();

Although you might want to adjust your CSS rather than put a space in the field.

input[type=text] {
    padding-left: 1em;
}

UPDATE

I notice in your comments you are putting in the twitter account and want a space after it... here is the solution.

$inputField = $("#inputField");

$inputField.val($inputField.val() + " ").focus();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜