views:

27

answers:

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

when the cursor focuses on the texbox i want to do 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
    });
});
A: 

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!

Felix Kling
thanks for the answer, but i want it without the condition!!! because the in my script the focus is set only when the link is clicked
getaway
@getaway: Then let the condition be. You don't have to use the code exactly like posted it ;) But this will overwrite the value of the field. Or you do it like in the second example. But still I don't understand how your code should work exactly. What if the link is clicked twice?
Felix Kling
my code is shown above, what happens is, its exactly like twitter, when someone clicks reply button then the @username is taken to the texbox and after i want a space, so to avoid the user typing @usernamegoing to a party!!! u see the going is attached to the @username, this makes the user @ someone else or someone that dnt exist!! thanks and sorry for taking long
getaway
thanks sorry a bit of a newbie in javascript, but the update still deosnt work!! i was thinking ading space in the html tags
getaway
@getaway: But the name gets already inserted into the text field or did this not work either?
Felix Kling
@felix the name deos get inserted into the text field but with no spaces.
getaway
@felix thanks mate it works your a genius!!! sorry for giving you a headache im such a newbie
getaway
@getaway: You're welcome! No worries, everyone started at some point ;) Happy coding!
Felix Kling
A: 

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();
Sohnee