Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6599385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:29:04+00:00 2026-05-25T18:29:04+00:00

I have this code right now: $(document).ready(function () { $(div#main-edit).click( function() { var cursorExists

  • 0

I have this code right now:

$(document).ready(function () {

$("div#main-edit").click( function() {
var cursorExists = $("input#cursor").length;
if (cursorExists == false){
   $("div#cursor-start").append("<input type='text' id = 'cursor' />");
   $("input#cursor").focus();
  }

});


  $("input#cursor").keydown(function() {
  $("div#cursor-start").enterText();
});

jQuery.fn.enterText = function(){
    alert("hello");
};

});

The function enterText is not called in this situation though. It seems like the fix should be easy for this. Is it because jquery finds nothing to select (input#cursor) when the document loads up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:29:05+00:00Added an answer on May 25, 2026 at 6:29 pm

    I think this code has problems because you are calling this;

    $("input#cursor").keydown(function() {
      $("div#cursor-start").enterText();
    });
    

    before the object input#cursor has been created by your code. Thus, it won’t find the object and can’t install an event handler for it. I would suggest you change your code to this which installs the event handler only after the object is created:

    $(document).ready(function () {
    
        $("div#main-edit").click( function() {
            var cursorExists = $("input#cursor").length;
            if (cursorExists == false) {
                $("div#cursor-start").append("<input type='text' id = 'cursor' />");
                $("input#cursor").focus();
                // install event handler, now that we know the object exists
                $("input#cursor").keydown(function() {
                    $("div#cursor-start").enterText();
                });
            }
        });
    
        jQuery.fn.enterText = function(){
            alert("hello");
        };
    });
    

    The alternative would be to use .live("keydown", function() {}) instead of .keydown() which allows you to install some event handlers before the object exists (you can read the jQuery doc on it to understand further).

    FYI, you get a lot worse performance in jQuery when you use selectors like “input#cursor” instead of just “#cursor”. The latter gets optimized to use document.getElementById(). The former does not and takes a much longer code path. Since there is only one object in the page with any given ID, there should be no reason to quality it with the tag type. Just use “#main-edit”, not “div#main-edit”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code right now : $(document).ready(function() { var ktitle = $('.hiddentwo').text(); $('div#tab2').load('morefour.php?title=ktitle');
I have right now this code: <ul><li class=listitem>text<li></ul> jQuery: $('.listitem').click(function() { $(#elname).text($(this).text()); $('#slists').css('visibility','hidden') $('#elname').css('visibility','visible')
Right now my code looks like this: <script type=text/javascript> $(document).ready(function() { $.ajaxSetup({ cache: false
Here is my code within (document).ready $('.checkfirst').click(function() { if ($(.textbox).is('.required')) { var x =
I have this code right now to get the row value from jquery grid..
Right now, I have this code where $obj_arr maybe contain array and an object.
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
The following code: jQuery(document).ready(function($) { function getBooks() { var query = ajax.php?do=allbooks; $.ajax({ dataType:
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
I have this code which compiles and works as expected: class Right {}; class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.