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 6838917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:39:18+00:00 2026-05-26T23:39:18+00:00

I wrote a script that allows users to click onto a table cell and

  • 0

I wrote a script that allows users to click onto a table cell and edit the value with jQuery. Basically, when they click the cell, the HTML in the cell gets replaced with an input box and any text that was previously in the cell gets added to the input. My issue is that whenever they click on the cell, the input doesn’t get focused and they have to click a second time to put the cursor in. I have tried a bunch of selectors with .focus() to try and put the cursor in but I’m not having any luck.

Here is the function that gets called when the user clicks on a cell:

function edit_cell()
        {   
            if($(this).hasClass('edit_box'))
                if(!$(this).hasClass('editable')){
                    $(this).addClass('editable');
                    string = $(this).text();
                    $(this).html("<input type='text' value='" + string + "'/>");
                }
        } 

Also here is a second function that adds a new <tbody> and a new table row, with the first cell containing an active input field.

function add_ex(){
     string = "<tbody><tr><td><input type='text'></td></tr></tbody>";       
     $(this).parents('tbody').before(string);
}
  • 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-26T23:39:19+00:00Added an answer on May 26, 2026 at 11:39 pm

    This works – just get an explicit reference to the new input element and .focus() on it:

    function edit_cell() {
        if ($(this).hasClass('edit_box')) {
            if (!$(this).hasClass('editable')) {
                $(this).addClass('editable');
                var input = $('<input type="text">').val($(this).text());
                $(this).html(input);
                $(input).focus();
            }
        }
    }
    

    Note that this also avoids the potentially error prone string concatenation when you create the <input> element.

    Working example at http://jsfiddle.net/alnitak/SpkZG/


    For your second snippet, create the elements straight away, put them into the DOM, then focus on the single element you need:

    var $tbody = $("<tbody><tr><td><input type='text'></td></tr></tbody>");
    $(this).parents('tbody').before($tbody);
    $tbody.find(':input').focus();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already wrote a PHP script that allows users to upload images but I
I'm trying to write a script that allows an admin of a photo uploading
I wrote a script that uses xcodebuild to generate an AdHoc build of an
I wrote a Python script that does some task to generate, and then keep
This is a crude korn shell script that someone else wrote. I don't know
I have a Perl script I wrote for my own personal use that fetches
I wrote a short bash script to complete a task that involves creating a
I have a Python script I recently wrote that I call using the command
I wrote a script using Ruby to click on the screen automatically on Windows's
All, I'm working on an application that allows authorized users to upload Excel spreadsheets

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.