Need to update various form fields at once with jquery?

There is a form with fields for firstname, lastname, email, country, along with edit icon and submit/cancel buttons.

When the user clicks on the edit icon in the top right corner, all values will be displayed in textboxes and the country will be shown in a select dropdown.

I have attempted some code but I am encountering issues where all values are being displayed in a single field and appearing multiple times. Can anyone assist me with this?

$('#edit').click(function() {
 var text = $('.text-info').text();
 var input = $('<input id="attribute" type="text" value="' + text + '" />')
 $('.text-info').text('').append(input);
 input.select();
 $('.btn').show();
 $('#edit').hide();
});
.width-400{width: 400px;margin: 0 auto;padding: 20px;}
.border{border:1px solid #000;}
input{margin-bottom: 10px; width: 100%;}
.btn{display:flex;margin-top: 10px;display: none;}
.font-icon{text-align: right;}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="border width-400">
<div class="font-icon"><a class="edit" href="#" id="edit"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a></div>
<form method="post" action="update.php">
<div class="form-group">
    <label for="fname" class="control-label">Firstname:</label><spa class="text-info">Narendra</span>
</div>

<div class="form-group">
    <label for="lname" class="control-label">Lastname:</label><span class="text-info">verma</span>
</div>

<div class="form-group">
    <label for="email" class="control-label">Email:</label><span class="text-info"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7a79785b7c767a727735787476">[email protected]</a></span>
</div>
    <select>
<option value="">Choose country</option>
<option>India</option>
<option>USA</option>
<option>UK</option>
</select>
<div class="btn">
<input type="submit" name="submit" value="save">
<input type="submit" name="submit" value="cancel">
</div>
    </form>
</div>

Answer №1

    $('#edit').click(function() {
     var text = $('.text-info').each(function (index,oneSpan){
                var input = $('<input type="text" value="' + $(oneSpan).text() + '" />')
     $(oneSpan).text('').append(input);
});
 

var country = $('.select-info').text();
  $('.select-info').hide();
  $('#select-elm').val(country).show();
 $('.btn').show();
 $('#edit').hide();
    });
.width-400{width: 400px;margin: 0 auto;padding: 20px;}
.border{border:1px solid #000;}
input{margin-bottom: 10px; width: 100%;}
  .btn{display:flex;margin-top: 10px;display: none;}
  .font-icon{text-align: right;}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="border width-400">
<div class="font-icon"><a class="edit" href="#" id="edit"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a></div>
<form method="post" action="update.php">
<div class="form-group">
    <label for="fname" class="control-label">Firstname:</label><span class="text-info">Narendra</span>
</div>

<div class="form-group">
    <label for="lname" class="control-label">Lastname:</label><span class="text-info">verma</span>
</div>

<div class="form-group">
    <label for="email" class="control-label">Email:</label><span class="text-info"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c1d1e1f3c1b111d1510521f1311">[email protected]</a></span>
</div>
<div class="form-group">
    <label for="country" class="control-label">Country:</label><span class="select-info">USA</span>
</div>
    <select id="select-elm" >
<option value="">Choose country</option>
<option>India</option>
<option>USA</option>
<option>UK</option>
</select>
<div class="btn">
<input type="submit" name="submit" value="save">
<input type="submit" name="submit" value="cancel">
</div>
    </form>
</div>

Points to Note: In the code, when using $('.text-info'), all nodes with this class are being selected. The text() method on it just concatenates them, resulting in a single string of values. After creating the input and appending it using

$('.text-info').text('').append(input);
, another array is returned by the text method, causing the append operation to be executed on all elements. This leads to a long string contained within an input that is appended multiple times to your spans.

Corrected Approach:

By utilizing each(function(){}) after fetching an array from $('.text-info'), we can iterate through each span individually. Simply create an input, retrieve the value from our individual span, empty it, and then populate it with our desired array one at a time.

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

Creating types for React.ComponentType<P> in Material-UI using TypeScript

I am currently working with Typescript and incorporating Material-UI into my project. I am trying to define the component type for a variable as shown below: import MoreVert from '@material-ui/icons/MoreVert' import { SvgIconProps } from '@ ...

Utilizing Ajax to dynamically update the color of a button when it is clicked from a different device

Currently, I am developing a program for my own use within my local community theater. The setup will involve a computer in the control room (Sound Booth) and another computer backstage. My plan is to create a grid of around 10 interactive "buttons" on th ...

Javascript issue: opening mail client causes page to lose focus

Looking for a solution! I'm dealing with an iPad app that runs html5 pages... one specific page requires an email to be sent which triggers the Mail program using this code var mailLink = 'mailto:' + recipientEmail +'?subject=PDFs ...

How to align buttons in the center of a Bootstrap grid column using Angular's *ngFor directive

I have been trying to center some buttons using the bootstrap 4 grid system within a column with a green border. Despite using justify-content-center, I have not been successful so far. <div class="row" style="border:1px solid red;"& ...

Send the latest version to a separate recipient

I am currently exploring the process of achieving the following: Within my dashboard, there are two distinct user types utilizing the system. One type initiates an action which triggers a database update through the use of .ajax() post on a different page ...

Do external JavaScript files exclusively serve as repositories for functions?

Hey there, I'm a beginner in Web Development and I have a question that might sound silly: If I decide to keep my scripts in an external .js file, would it essentially just serve as a container for functions? For instance, if I attempt to include cod ...

The functionality of JQuery's .hover() is disabled once an HTML onClick() event is activated

I am currently working on a webpage and attempting to incorporate JQuery into it for the first time. However, I seem to be encountering some issues that I believe might have simple solutions. Background Information My JQuery code only contains one event l ...

Unspecified locale with Next.js router

Despite extensive research and multiple attempts, I have not been able to find a solution to my problem. That's why I am reaching out for help again. Currently, I am working on a local project and I would like to implement Internationalized Routing f ...

What is the best way to update a value and trigger a re-render in ReactJS?

this.state={ value: 'v1', newValue: '', } componentDidMount = () => { let nV = this.state.value + 'received'; this.setState({ newValue: nV, }); } tabClick = (val) => { this.setState({ value: val, ...

Using `display:table-cell` does not function correctly when set to a 100% width

I'm having an issue with aligning text vertically inside a div: display:table-cell; vertical-align: middle; While the text aligns properly, the div (which has a width of 100%) is shrinking in width. What could be causing this problem and is there a ...

Developing a cascading dropdown feature for ASP.NET MVC using JSON

I am currently working on implementing a cascading drop-down list in ASP.NET MVC by following a tutorial. However, I'm encountering an issue where the first drop-down box is not loading with manufacturer data. I suspect there may be an error with the ...

Jump straight to the top of the page with just a click using the Google Maps Store Locator

When I use my Store Locator and click on an anchor like "Zoom Here," "Directions," or "Street View," the href hash always brings me back to the top of the page. How can I prevent this from happening? I've tried examining the minified source code for t ...

Can Jquery's 'on' method function properly on elements that are added with element.innerHTML?

On our website, we have a 'content' div where the HTML is inserted. For example: content.innerHtml = 'a large amount of HTML with divs and tables' Now, I am trying to add a jQuery event handler to one of the inserted elements by using ...

Mistake in Timestamp Separation - JavaScript

I have a Timestamp retrieved from MSSQL and displayed on a Webclient. The timestamp reads: Thu Jan 01 18:00:00 CET 1970, however, I am only interested in the time format: 18:00 or 18:00:00 (preferably the former). <script> var timestamp = ...

Streamline email error management within nested middleware functions

I have implemented an express route to handle password resets, which includes finding the user and performing some error handling. However, I am now faced with the challenge of adding additional error handling within a nested function, and I am uncertain a ...

Updating Bootstrap Indicators with jQuery on Click Event

Unfortunately, I am unable to share an image due to limited internet data. My goal is to switch each image to its sprite equivalent. There are three list items that I'm struggling to change because they each have two classes that need to be updated. ...

Send form with information dynamically added via AJAX in Laravel

Below is the segment of HTML I am working with: <div class="box-body" style="display:none;" id="claimFreightDetails"> <input type="text" disabled class="form-control" id="pulledProNumber" name="pulledProNumber"><br> ...

"An issue has arisen in MongoDB when attempting to save data, resulting in an error message

Having trouble inserting data into MongoDB with Node.js. Encountering an error in the final line of code. Here are the execution logs: { _id: 56e90c1292e69900190954f5, nfs: [ 'ebdp1', 'ebdp2', 'ebdp3', 'ebdp4' ], s ...

Choose the value of the adjacent text input with jQuery

I am working with dynamic checkboxes and corresponding text inputs. My goal is to alert the value of the text input when a checkbox is checked. However, I am having trouble retrieving the correct value of the text input associated with each checkbox. Addit ...

Creating a stylish gradient text color with Material-UI's <Typography /> component

Is there a way to apply a gradient font color to a <Typography /> component? I've attempted the following: const CustomColor = withStyles({ root: { fontColor: "-webkit-linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)", }, })(T ...