admin管理员组

文章数量:1429931

I have a Code that I got form my first post :Need help about a function

I modified this code and trying to work like how I want .But I got Stuck in one point .

JSfiddle LINK : /

Here is the Code :

HTML

<button id="createDiv">Start</button>
<div id="results"></div>

CSS

    #createDiv, #results span { cursor: pointer; }
#results div {
    background: #FFA;
    border: 1px solid;
   width:auto;
}
#results input[type=text] {
    border: none;
    display: none;
    outline: none;
}

JS

    //  Call for document .onload event
$(function() {
    //  Normal Click event asignement, same as $("#createDiv").click(function
    $("#createDiv").on("click", function(e) {
        //  Simply creating the elements one by one to remove confusion
        var newDiv = $("<div />", { class: "new-folder" }),  //  Notice, each child variable is appended to parent
            newInp = $("<input />", { name: "inpTitle[]",style:"display:block ;border:solid 1px #fa9a34", type: "text", value: "Unnamed Group", class: "title-inp" }).appendTo(newDiv),
            newSpan = $("<span />", { id: "myInstance2",style:"display:none", text: "Unnamed Group", class: "title-span" }).appendTo(newDiv);
        //  Everything created and seated, let's append this new div to it's parent
        $("#results").append(newDiv);
    });

    //  the following use the ".delegate" side of .on
    //  This means that ALL future created elements with the same classname, 
    //    inside the same parent will have this same event function added
    $("#results").on("click", ".new-folder .title-span", function(e) {
        //  This hides our span as it was clicked on and shows our trick input, 
        //    also places focus on input
        $(this).hide().prev().show().focus();
    });
    $("#results").on("blur", ".new-folder .title-inp", function(e) {
        //  tells the browser, when user clicks away from input, hide input and show span
        //    also replaces text in span with new text in input
        $(this).hide().next().text($(this).val()).show();
    });
    //  The following sures we get the same functionality from blur on Enter key being pressed
    $("#results").on("keyup", ".new-folder .title-inp", function(e) {
        //  Here we grab the key code for the "Enter" key
        var eKey = e.which || e.keyCode;
        if (eKey == 13) { // if enter key was pressed then hide input, show span, replace text
            $(this).hide().next().text($(this).val()).show();
        }
    });
})

WHAT it does now is , when I click the Start Button Create a input Box which Can be Rename and can save as text . BUT what I am wanting is .. WHEN I click the Start button it will Create input With BLINKING cursor which is not here now . Now it bee active when I clicked on it . But I am wanting it should Start blinking When it's Created . Is possible with any jquery or java scripts ?

I have a Code that I got form my first post :Need help about a function

I modified this code and trying to work like how I want .But I got Stuck in one point .

JSfiddle LINK : http://jsfiddle/saifrahu28/qzKWD/

Here is the Code :

HTML

<button id="createDiv">Start</button>
<div id="results"></div>

CSS

    #createDiv, #results span { cursor: pointer; }
#results div {
    background: #FFA;
    border: 1px solid;
   width:auto;
}
#results input[type=text] {
    border: none;
    display: none;
    outline: none;
}

JS

    //  Call for document .onload event
$(function() {
    //  Normal Click event asignement, same as $("#createDiv").click(function
    $("#createDiv").on("click", function(e) {
        //  Simply creating the elements one by one to remove confusion
        var newDiv = $("<div />", { class: "new-folder" }),  //  Notice, each child variable is appended to parent
            newInp = $("<input />", { name: "inpTitle[]",style:"display:block ;border:solid 1px #fa9a34", type: "text", value: "Unnamed Group", class: "title-inp" }).appendTo(newDiv),
            newSpan = $("<span />", { id: "myInstance2",style:"display:none", text: "Unnamed Group", class: "title-span" }).appendTo(newDiv);
        //  Everything created and seated, let's append this new div to it's parent
        $("#results").append(newDiv);
    });

    //  the following use the ".delegate" side of .on
    //  This means that ALL future created elements with the same classname, 
    //    inside the same parent will have this same event function added
    $("#results").on("click", ".new-folder .title-span", function(e) {
        //  This hides our span as it was clicked on and shows our trick input, 
        //    also places focus on input
        $(this).hide().prev().show().focus();
    });
    $("#results").on("blur", ".new-folder .title-inp", function(e) {
        //  tells the browser, when user clicks away from input, hide input and show span
        //    also replaces text in span with new text in input
        $(this).hide().next().text($(this).val()).show();
    });
    //  The following sures we get the same functionality from blur on Enter key being pressed
    $("#results").on("keyup", ".new-folder .title-inp", function(e) {
        //  Here we grab the key code for the "Enter" key
        var eKey = e.which || e.keyCode;
        if (eKey == 13) { // if enter key was pressed then hide input, show span, replace text
            $(this).hide().next().text($(this).val()).show();
        }
    });
})

WHAT it does now is , when I click the Start Button Create a input Box which Can be Rename and can save as text . BUT what I am wanting is .. WHEN I click the Start button it will Create input With BLINKING cursor which is not here now . Now it bee active when I clicked on it . But I am wanting it should Start blinking When it's Created . Is possible with any jquery or java scripts ?

Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked May 16, 2013 at 14:44 S RaihanS Raihan 3872 gold badges9 silver badges18 bronze badges 1
  • Another improvement would be to use placeholder instead of value for input (text). – Ionică Bizău Commented May 16, 2013 at 14:57
Add a ment  | 

3 Answers 3

Reset to default 2

You need to use the focus() event.

$("#results").append(newDiv);
newInp.focus();

You should use newInp.focus() to give the textbox focus, but you presumably also want the cursor at the end?

Use the following to achieve that:

$.fn.setCursorToTextEnd = function() {
    $initialVal = this.val();
    this.val('');
    this.val($initialVal);
};

Then do:

newInp.focus().setCursorToTextEnd();

Credit should go to Gavin G for the setCursorToTextEnd()

I've applied this to your fiddle - http://jsfiddle/qzKWD/3/

You need the focus() event.

A solution would be finding the input inside of newDiv element that is appended to #results, and then focusing on it.

// Inside of click handler
$("#results").append(newDiv);
newDiv.find("input").focus();

Another solution is to focus directly on newInp element that is same <input>.

// Inside of click handler
$("#results").append(newDiv);
newInp.focus();

JSFIDDLE

本文标签: javascriptClick a button to open a input field with Blinking CursorStack Overflow