What causes a table to lose focus in this situation?

I have a form with a table containing 3 columns. I need to use the fadeIn and fadeOut functions on the textbox in the third column based on the checkbox's state. However, when I enable the checkbox, the width of the table column changes. How can I fix this issue? Check out the code here

Here is the code:

<form action="" method="POST>
     <table width="50%" cellpadding="4" cellspacing="1" border="1">
         <tr>
             <td width="5%" align="center"><input name="chk_surname" id="chk_surname" type="checkbox" onclick="enable(this.id,'surname','td_surname')"></td>
             <td width="10%" align="center">Surname</td>
             <td width="35%" class="td_surname" style="display:none;" align="center"><input type="text" name="surname" id="surname" /></td>
         </tr>
     </table>
</form>

Here is the JavaScript code:

<script type="text/javascript">
    function enable(id,name,className)
    {
        $(document).on('change','#'+id, function() 
        {
            var checked = $(this).is(":checked");

            var index = $(this).parent().index();

            if(checked) {
                $('.'+className).fadeIn(100);
            }
            else {
                $('.'+className).fadeOut(100);
            }
        });
    }
</script>

Screenshot:

Answer №1

Change the visibility of the input box smoothly instead of the td.

Give this a shot:

HTML:

<form action="" method="POST">
     <table width="50%" cellpadding="4" cellspacing="1" border="1">
         <tr>
             <td width="10%" align="center"><input name="chk_surname" id="chk_surname" type="checkbox" onclick="enable(this.id,'surname','td_surname')"></td>
             <td width="20%" align="center">Surname</td>
             <td width="70%" class="td_surname" align="center"><input style="display:none;" type="text" name="surname" id="surname" /></td>
         </tr>
     </table>
</form>

JS:

function enable(id,name,className)
{
        var el = '#'+id;
        var checked = $(el).is(":checked");

        var index = $(el).parent().index();

        if(checked) {
            $('#'+name).fadeIn(100);
        }
        else {
            $('#'+name).fadeOut(100);
        }
    }

Demo: http://jsfiddle.net/W2wQF/

The reason for the current behavior of the original code is due to the third column being removed causing the table to resize. With the revised code, only the contents of the third column are toggled, ensuring no resizing occurs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most efficient way to write this code?

When I find myself surrounded by a ton of PHP code, instead of exiting PHP I tend to include variables directly within strings like this: echo "This is how I use a ".$variable." inside a string"; However, I wonder if it would be more efficient to actuall ...

What could be causing my code to become unresponsive when using a for loop compared to a loop with a specific

After spending a solid 4 hours on it, I finally managed to figure it out. There were no errors popping up, so I resorted to using the debug feature which unfortunately didn't provide much insight. Without any error messages to guide me, I wasn't ...

Unable to modify translation values in an existing ngx-translate object

I am facing an issue where I am unable to change the value of an object property within a translation JSON file using ngx translate. Despite attempting to update the value dynamically when receiving data from an API, it seems to remain unchanged. I have t ...

This error message occurred: "Jquery Uploader Call to undefined method UploadHandler::query()"

I'm currently setting up a Jquery uploader and I want it to automatically add the file name to my MySQL database once the upload is complete. https://github.com/blueimp/jQuery-File-Upload/wiki/Working-with-databases Inside the uploadhandler.php file, ...

Display HTML content using AJAX within a div element

Within my HTML code, I have the following: <li class="register"><a href="">Registreer</a></li> as well as <div id="content"> </div> I attempted to load an HTML file into the div using the code below in the header se ...

Create the correct structure for an AWS S3 bucket

My list consists of paths or locations that mirror the contents found in an AWS S3 bucket: const keysS3 = [ 'platform-tests/', 'platform-tests/datasets/', 'platform-tests/datasets/ra ...

Wordpress AJAX dilemmas

Currently facing issues with integrating Wordpress and Ajax. Despite enabling jQuery noconflict and ensuring that the effects are functional, the Ajax request simply does not work. What could potentially be causing this problem? Thank you for any insight ...

Applying the second style sheet in the child template to customize style attributes and override existing

Currently, I am in the process of building a website using Python and Django. The template HTML page I am working on is called Speakers.html, which extends from Base.html. The base stylesheet used is base.css. While Speakers.html is displaying with the st ...

Is there a way to trigger a server method when the user exits the page?

Is there a way to automatically remove a temporary file from the server when a user closes the page? I don't have access to a default server-side callback for this action. I attempted to call a server-side method using an implementation of ICallbackE ...

Chromium browsers exhibit strange behavior with AngularJS orderBy, causing issues with clearing sorting selections

There is a particular issue that is really frustrating me in one of my projects. The code I have is quite similar to this example (I tried to replicate it as closely as possible, but the actual code doesn't seem to be relevant because I can't re ...

The jquery object is coming back as undefined when used in the second `.forEach()`

Hey there, I'm encountering an issue with displaying JSON data (questions and answers). I am using a forEach loop to iterate through the questions and another forEach loop to go through the answers. However, the answers are showing up as undefined eve ...

Fetching jQuery library via JavaScript

Having trouble loading the JQuery library from a JavaScript file and using it in a function. JS1.js $(document).ready(function () { //var id = 728; (function () { var jq = document.createElement('script'); jq.type = 'te ...

What is a more efficient way to avoid duplicating code in javascript?

Is there a way to avoid repeating the same code for different feeds? I have 8 feeds that I would like to use and currently, I am just incrementing variable names and feed URLs. <script type="text/javascript> function showFeed(data, content ...

Scoped variable in Typescript producing a generated Javascript file

I'm currently learning TypeScript through an online course, and I've encountered a problem that seems to be related to a VSCode setting. Whenever I compile app.ts, it generates the app.js file, but I immediately encounter a TypeScript error. It& ...

How can we enable Material UI tooltip based on certain conditions?

In my React component utilizing Material UI, I have the code snippet below: const MyButton = ({ warningText }) => ( <Tooltip title={warningText}> <Button>Do action</Button> </Tooltip> ) At present, an empty tool ...

Dynamic Bootstrap User Authentication Form

Currently, I am in the process of constructing a login form for my web application and have opted to use Bootstrap for its styling. To ensure that the screen is responsive, I've integrated the following "Meta Tag" into the structure: <meta name="v ...

Is it possible to replace text on click using CSS instead of jQuery?

My new project involves creating a flashcard system. The idea is that when a question is clicked, it reveals the answer and hides the question. To achieve this, I am currently using the following code: jsFiddle <p id="shown">What is stackoverflow?&l ...

Tips for enhancing contrast in MUI dark theme modals

Currently, I am implementing MUI dark mode for my Next.js application. While the MUI modal functions perfectly in light mode, I am struggling with visibility issues when using it in dark mode. The contrast is not strong enough, making it difficult to disti ...

Can someone help me uncover the previous URL for login using just JavaScript? I've tried using document.referrer but it's not giving me the

Currently, I am utilizing express with pug templates and pure JavaScript. In order to enhance the user experience of my log in system, I would like to save the URL that someone came to the login page with, so that I can redirect them back to it once they h ...

Alignment problem

In my design, I have a toolbar with flex display and I am trying to center my span element within it. However, I seem to be missing something in the CSS. CSS .toolbar { position: absolute; top: 0; left: 0; right: 0; height: 60px; d ...