Tips for continuously duplicating a value in every textbox until the final one

I am looking to implement a feature where the value of a double-clicked textbox is repeated until the end of the textbox. In my form, there are ten text boxes with different values. When I double click on the third textbox, I want the value of the third textbox to be repeated in all textboxes from the third to the tenth, while the first and second textboxes retain their original values. I have provided the code snippet below for reference.

http://codepen.io/nachicode/pen/dXKaaA/

Code Snippet:

HTML

<table>
  <tr>
    <th>One</th>
    <th>Two</th>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="1" />
    </td>
    <td>
      <input Type="text" Value="1" />
    </td>
  </tr>
  // More HTML code here
</table>

JQUERY

$(document).ready(function() {
  $("input").dblclick(function() {
    //Code implementation goes here
  });
});

Answer №1

Here is a suggested method:

// To implement the following approach:
// Locate all <input> elements inside a <td> element,
// Connect the on() method's anonymous function 
// as the handler for the 'dblclick' event:
$('td input').on('dblclick', function() {

  // Identify the parent <tr> or the double-clicked element:
  var currentRow = $(this).closest('tr'),

    // Retrieve all subsequent siblings of the current <tr>,
    // and include the current <tr> in that selection
    // using addBack():
    laterSiblingRows = currentRow.nextAll().addBack(),

    // Find relevant <input> elements within the <tr>
    // elements:
    laterInputs = laterSiblingRows.find('input');

  // Update the value of those <input> elements to
  // match the value of the double-clicked element:
  laterInputs.val(this.value);
})

$('td input').on('dblclick', function() {
  var currentRow = $(this).closest('tr'),
    laterSiblingRows = currentRow.nextAll().addBack(),
    laterInputs = laterSiblingRows.find('input');

  laterInputs.val(this.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>One</th>
    <th>Two</th>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="1" />
    </td>
    <td>
      <input Type="text" Value="1" />
    </td>
  </tr>
  ...
  <tr>
    <td>
      <input Type="text" Value="10" />
    </td>
    <td>
      <input Type="text" Value="10" />
    </td>

  </tr>
</table>

JS Fiddle.

Additional Resources:

Answer №2

Is this what you had in mind?

$(document).ready(function() {
  $("input").dblclick(function() {
    var index = $(this).closest('td').index() + 1;
    var rows = $(this).closest('tr').nextAll().addBack();
    rows.find('td:nth-child(' + index + ') input').val($(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>One</th>
    <th>Two</th>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="1" />
    </td>
    <td>
      <input Type="text" Value="1" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="2" />
    </td>
    <td>
      <input Type="text" Value="2" />
    </td>
 

Answer №3

Give this a shot:

$("input").dblclick(function(){
        var inputValue = $(this).val();

        for(counter=inputValue; counter<=10; counter++)
        {
            $('input[value="'+counter+'"]').val('hello');
            //$('input[value="enter highlight"]')
        }
    });

Answer №4

Instead of providing direct code solutions, a possible approach could be assigning unique IDs to your input elements like "box1", "box2", and so on. By double-clicking an input, you can retrieve its ID, determine the index, increment it, and then reference the subsequent inputs using the "box" naming convention until either no element with that ID is found or a certain limit, such as 10, is reached.

Here's a rough pseudo-code illustration:

var clickedBoxId = this.ID; // Assuming "this" refers to the clicked box
var index = clickedBoxId.Replace('box','');

for( int i = index; i <= 10; i++ )
{
    var anotherBox = document.getElementById('box' + i);
    if( anotherBox !== 'undefined' )
    {
        anotherBox.value = this.value;
    }
}

Keep in mind that this pseudocode might need adjustment when translated into actual JavaScript implementation since I'm not a frequent JS user. Therefore, please use it as a guide rather than a ready-made solution.

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

How can you organize an array into rows and columns for easier viewing?

Hopefully everything is clear. I am open to making changes if needed to address any important points. In one of my components, the array items are displayed within cards as shown below: <b-card-group deck class="mb-3"> <b-card border-variant ...

What is the best way to keep a div element fixed at the top of a webpage?

I am facing an issue with a div that contains three SVG files layered on top of each other and meant to be centered on the page. Ideally, when the user scrolls down, I want these SVG files to stick to the top and remain there. In my CSS attempts, I have u ...

How can I prevent one button from taking precedence over another button even though they have different classes?

I am a newcomer to javascript and jquery and have been grappling with this issue for some time. I have implemented two buttons in a table row - a clear button to clear all forms and a reset button to revert to initial values. However, I am facing a problem ...

What's the best way to place my image inside a Bootstrap Accordion so that it is housed within the accordion-item?

I've been facing an issue with a Bootstrap Accordion. Everything works fine, except when I try to insert an image <img src="https://placehold.co/600x400" class="img-fluid" /> into an accordion-item <div class="accord ...

Import information from a MySQL table into a single input field

I am currently working on a project that involves retrieving data from a MySQL database and displaying it in a single field. Below is the MySQL code I am using: My goal is to load all the data from $row['db_numbers']; into the field. <input t ...

Header navigation using jQuery

I'm encountering an issue with the final exercise in the jQuery course on Codecademy. My goal is to replicate the navigation header as accurately as possible, but I'm struggling to figure out how to keep the selected div highlighted after it&apo ...

Flipping a 2-dimensional box using CSS and incorporating cascading flex divs

*Please read the note: This question is a bit lengthy as I wanted to explain my thought process. For those who prefer not to read everything, I have created codepens for easy reference. Essentially, I am looking to achieve CSS box flipping effects like t ...

What steps are involved in integrating Bootstrap and jQuery into a Vue Webpack-Simple template?

I am trying to integrate Bootstrap 3 into my Vue app. Here is what I have done so far: vue init webpack-simple npm install jquery bootstrap After this, I added the following to webpack.config.js: module.exports = { ... plugins: [ new webpa ...

Display additional text when hovering over an object

Is there a way to make my text expand and display all of its content when hovered over, especially for those sections that are longer than the div size? I currently have some code implemented, but it seems to reveal the text horizontally. I am looking fo ...

Tips for showcasing div content on a separate line rather than inline within a collapsible <a> tag

I am currently working on integrating a sidebar using bootstrap 5. When I click on the user initials, I want something like this to happen (collapse): https://i.sstatic.net/ejbhD.png However, the result I'm getting is different: https://i.sstatic. ...

Divide the full width into three divisions, each taking up 33% of the total width

I have been experimenting with using 3 divs to create a table-like layout with 3 columns. I set each div to occupy 33% of the width of the page, and it worked well until I tried to add padding to move the text away from the border. This caused the third di ...

Removing the Yellow Highlight on Input Field Following Email Autocomplete in Chrome

My username-password form is styled and working perfectly, but there's an issue that arises when I log in multiple times. Chrome automatically fills in my email, turning the username textbox yellow. It doesn't seem to happen with Firefox or Safar ...

The server failed to respond to the Angular HTTP request

To store data in my database, I have created a function in my component.ts file that invokes a service: ajoutText(newtext: String) { this.dataService.sendtext(newtext); } On the HTML side, it looks like this: <form class="form"> <mat-form-f ...

Choose only one option from the dropdown menu at a time within the specified div

I attempted to utilize the "setSelected" option on my multiselect feature, but I noticed that it does not function with div elements (at least I could not make it work myself). I am endeavoring to create two synchronized multiselects using this example: b ...

Scaling Vuetify icons dimensions

During the development of an app using Vuetify, I encountered challenges in changing the default colors set by Vuetify. After some trial and error, I came across a solution on: https://github.com/vuetifyjs/vuetify/issues/299 The solution involved adding ...

What is the best way to eliminate the blue color from a Boostrap 5.3 accordion item when it is opened?

As shown in these two images, I am looking to remove the blue color and blue border when opened: https://i.sstatic.net/vOIna.png https://i.sstatic.net/7eLfK.png <div class="accordion-item"> ...

Switching the markLine in vega lite to a markBar causes it to lose its sorting arrangement

I have created the following data visualization: data = [{"student_name": "student 0", "e": "100.15", "d": "127.81"}, {"student_name": "student 1", "e": "100.30", "d": "189.94"}, {"student_name": "student 2", "e": "100.15", "d": "105.33"}, {"student_nam ...

Update button text in real-time using JavaScript

I am currently working on a dropdown list that contains 5 elements displayed inside a button when pressed. I want the button to show a default text "Filter by:" before displaying the selected tab value. Additionally, I need the caret symbol to be visible. ...

What could be causing the padding of my anchor element to extend outside the boundaries of its parent list

In the provided images, it is evident that the <li> element is failing to expand and is disregarding the padding of the parent element.</p> <p><a href="https://i.stack.imgur.com/4ZH65.png" rel="nofollow noreferrer"></a></p& ...

Having trouble getting any results from a JSON response using the jQuery Tokeninput plugin in PHP?

I am currently trying to implement the JQuery Tokeninput feature on my website. However, when I enter text into the search field, nothing appears and it just continues searching indefinitely... After reviewing the documentation, I noticed that the expecte ...