Conversion of Element Types in JavaScript

How can I make a button add an input field when clicked, and then turn into another button or element once the input is submitted?


<button>add tag</button>
<input type="text" name="" placeholder="tag">

$(document).ready(function () {
        $('button').click(function () {
            $("button").before("input");
        });
    });

Answer №1

Give this a shot

$(document).ready(function() {
  $('button').click(function() {
    $("body").before("input");
  });
  $("input").keyup(function(e) {
    if (e.keyCode == 13)
      $(this).attr("type", "button");
    $(this).val('button')
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag">

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

The HTML video element only displays half of the video on Internet Explorer

Recently, I undertook a project that required me to replicate the exact content from https://www.google.com/atap/project-jacquard/. The issue arose when I used a video tag to play a background video, as it only displayed half of the video in Internet Expl ...

I'm having trouble getting my application to output to the console. What could be the issue?

In my cr-route.js file, I have a function that ensures the user is authenticated before displaying their name. module.exports = function (app, passport) { // ===================================== // HOME PAGE (with login links) ======== // == ...

Leveraging jQuery to dynamically load JSON data

Does anyone know how to properly handle a variable on a page that looks like this? var userdetails = "{ 'id': '1', 'username': 'me', }"; I am trying to call a function to load the id and username from this vari ...

The failure of the data-target selector in Bootstrap collapse feature

I've been attempting to collapse multiple targets with just one click. The Bootstrap documentation suggests that this can be achieved by specifying a matching CSS selector with the data-target attribute. However, it seems that the collapsing only work ...

Tips for enabling resize functionality on individual components one at a time

Check out my Codepen link for the resizable event While using Jquery UI resizable, everything is functioning correctly. However, I am now looking to have the resizable event activate one block at a time. When another block is clicked, the resizable event ...

I'm looking to use node.js to post an image on my blog

I am currently in the process of setting up a new blog using node.js. Everything is working smoothly on my local setup, but I am facing an issue when it comes to adding images to my articles. Despite utilizing bootstrap for input display, I am unable to sa ...

What is the best way to send a selected value from a dropdown in a Jade template to a REST API in a Node.js application

I am working with a Jade template that includes two dropdown menus and a Load button. The goal is to send the selected values from these dropdowns to my Node API when the user clicks on the Load button. Here's the code I have been trying: extends lay ...

Iterate through a list of items using ngFor directive in Angular, and

How can I apply an active class to the selected item in a ngFor loop without toggling it on every item? <div class="mb-2 item rounded bg-white border" *ngFor="let item of keys(); let i = index" (click)=&qu ...

Can the JQGrid subgrid URL be modified to change the parameter value from "?Id=id"?

I'm currently working on setting up a JqGrid with a subgrid like this... jQuery(document).ready(function() { jQuery("#list").jqGrid({ url: '/OrganizationalUnit/FindAll/', datatype ...

Display and Conceal Selected Choices

I have designed a simple form with two dropdown menus. Both menus have the same options: <option value='1234:0'>Closed</option> <option value='4567:2'>Open</option> <option value='6857:1'>Dead< ...

There seems to be an issue with the accurate calculation of the screen width while utilizing the scrollbar-gutter

Please take note: The scrollbar-gutter: stable; property is not compatible with Safari. Additionally, the issue seems to be specific to Chrome and works fine in Firefox. I have observed some unusual behavior when attempting to position elements fixed to t ...

Deactivating Timeout Requests in Koa Server

I keep encountering a connection reset error. I strongly believe it's stemming from a lengthy REST request, causing it to timeout. { [Error: socket hang up] code: 'ECONNRESET' } Is there a method to deactivate request timeouts within Koa, ...

How to fix a Rails 4 error when using Ajax to reload div content and where to properly place the remote: true

I am in the process of updating a specific section on my webpage periodically. This particular section is located in my index.html.erb file and has the following structure: <div id="shared_tasks_div"> <ul> <% @canEditTasks.each do |elem| ...

Tips for creating query requests in ExpressJS

Can someone provide the correct command to write in an ExpressJS file in order to expose a single HTTP endpoint (/api/search?symbol=$symbol&period=$period)? Here is what's currently working: app.get('/api/search/', (req, res) => ...

When the HTML and PHP code keeps running, the JavaScript text on the page updates itself

I was experimenting with threading in different languages like Java, PHP, and JavaScript. I am aware that JavaScript is single-threaded and PHP lacks robust threading capabilities. Below is the code snippet I have been working on: <body> <p id= ...

Is there a way to show an incorrect username and password message directly above the sign-in form?

Is there a way to have an error message display just above the sign-in form if the username and password are incorrect? I don't want it to redirect to another page, but simply show the error right above the form in my code. <!Doctype html> < ...

What is the best way to add spacing between each button in a Bootstrap 5 navbar?

Is there a way to add spacing between each button in the bootstrap 5 navbar? Currently, the buttons are too close to each other, and I would like to create some space between them. How can I achieve this? <link href="https://cdn.jsdelivr.net/npm/ ...

Material UI - sending an icon as a property

I need help with inserting a material-ui icon into my component using props. Can you please advise on what I might be doing wrong? I am struggling with passing the icon down in JSX, and here is the attempt that is not working: This code snippet shows me t ...

Restart simplemodal

In relation to this jQuery plugin found at , I am facing a situation with my ajax function where it takes some time to retrieve the data - therefore, I would like to display a spinner in the modal div. $("#basic-modal-content").modal(); $("#basic-modal- ...

Apply a specific class to the div elements that have an image with a designated width

Hello, let's dive into the code. <div class="nyhet"> <img src="#" width="785" class="attachement-full" /></div> <div class="nyhet"> <img src="#" width="390" class="attachement-full" /></div> <div class="nyhet"&g ...