Show the user's input within a clickable button

I'm trying to create a functionality where I have an input field and a button next to it. When I type something in the input field and click on the button, I want the result to be displayed in another button. Here is what I have attempted so far:

function add_keyword() {
  var keyword_value = (document.getElementById("keyword").value);
  var result = keyword_value;

  document.getElementById("btnresult").value = result;
}
#btnresult{
  display: none;
}
<button type="button" class="btn btn-default" name="clickbtn" value="Add Keyword" onclick="add_keyword()">Add</button>

<div class="input-group">
  <input type="text" class="form-control" id="keyword" name="keywordbox"/>
</div>

<button type="button" id="btnresult" class="btn btn-default">Result will show here</button>
    

Answer â„–1

Check out this link for more details

It seems like the solution you are looking for...

Here is a simplified version of the code snippet:

<div class="container">
  <input type='button' value='Click Me' data-action='add' /><br>
  <input type='text' data-input='text' />
</div>

<ul class='list-buttons' data-list='buttons'>
  <!-- where should these buttons go? -->
</ul>


The original question included jQuery as one of the tags, so I incorporated it into the solution

// caching elements for reuse
var $container = $('.container');
var $clickButton = $container.find('[data-action="add"]');
var $textInputField = $container.find('[data-input="text"]');

var $buttonList = $('[data-list="buttons"]');

$clickButton.on('click', function() { 
  var inputValue = $textInputField.val(); 
  $buttonList.append('<li><button>' + inputValue + '</button></li>'); 
  $textInputField.val(''); 
});

Answer â„–2

You're on the verge of success, just remember to reveal the button you initially hid and update the innerHTML property instead of setting a value for the button. Buttons display text between their tags, not values.

Here are the changes I made:

function add_keyword() {
    var keyword_value = (document.getElementById("keyword").value);
    var result = keyword_value;
    // Changed from .value to .innerHTML
    document.getElementById("btnresult").innerHTML = result;
    // Changed style display to 'block'
    document.getElementById("btnresult").style.display = "block"
}
#btnresult{
    display: none;
}
<button type="button" class="btn btn-default" name="clickbtn" value="Add Keyword" onclick="add_keyword()">Add</button>
<div class="input-group">
    <input type="text" class="form-control" id="keyword" name="keywordbox"/>
</div>
<button type="button" id="btnresult" class="btn btn-default">input value should be here</button>
    

Furthermore, there are areas in your code that could use enhancement, as described below:

function add_keyword() {
    // No need for parentheses around the document.getElement function.
    var keyword_value = document.getElementById("keyword").value;
    // You can directly assign the value without creating a new variable, but it's good practice to store elements in variables for reuse.
    var btn = document.getElementById("btnresult");
    btn.innerHTML = keyword_value;
    btn.style.display = "block"
}

EDIT: To meet OP's objective of creating a new button with the input content, here is an updated version that generates a new button for each input.

function add_keyword() {
    var keyword_value = document.getElementById("keyword").value;
    // Create a new button element.
    var btn = document.createElement("button");
    // Set its content to the keyword from the input.
    btn.innerHTML = keyword_value
    // Append it to the body.
    document.body.appendChild(btn);
}
<button type="button" class="btn btn-default" name="clickbtn" value="Add Keyword" onclick="add_keyword()">Add</button>
<div class="input-group">
    <input type="text" class="form-control" id="keyword" name="keywordbox"/>
</div>

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

Upload files using Ajax

Looking to implement Ajax and PHP for file upload functionality. The form includes an <input type="file"> tag, and I aim to enable users to upload a file without having to refresh the page. Any guidance on how to achieve this? While a refresh is no ...

Navigating from Page 1 to Page 2 and then returning to Page 1 using the browser's back button causes Page 1 to malfunction in NextJS version 13

I am currently using next version 13.4.5 and implementing routing with typescript through /app. On my first page, I have a <Link> (next/link) element that takes me to Page 2. However, when I use the browser back button to return to page 1, the layou ...

What is the process for including icons on buttons?

I have been researching how to use Font Awesome software to incorporate icons into my HTML elements, but I am uncertain about the implementation process for my specific case. Currently, I have created a basic webpage with buttons and I would like to includ ...

Presentation: Positioning Next Buttons Beside Slide Images While Maintaining Responsiveness

I am facing a challenge with my slideshow project. The slideshow consists of images and text on each slide, along with previous and next buttons that are aligned within the parent container. I am trying to align these buttons at the midpoint of each image ...

Issues arise with tabbed content as default content fails to display and classes are not added upon clicking

I'm currently working on a tabbed module that consists of three tabs. Here's how it operates: When the user clicks on a carousel_element, it reveals carousel_hidden-text within that div and displays it in another div called carousel_quote. I&ap ...

Conceal content within a bullet point

I'm encountering an issue with this specific element. HTML <div class="navbar slide-menu"> <div class="container"> <ul class="nav navbar-default"> <li class="brand"><a href="#">Menu</a></li> ...

Updating the total on the Infusionsoft order form can be achieved using either Jquery or custom code

Is there a way to automatically calculate and update the total amount on the order form based on user input in two quantity fields? The price should increase as the user adjusts the quantity. I've been searching for a solution, but haven't found ...

JavaScript can be used to deactivate the onclick attribute

Is there a way to deactivate one onclick event once the other has been activated? Both divs are initially hidden in the CSS. I'm new to programming, so any help is appreciated. Markup <a id="leftbutton" href="#" onclick="showDiv(this.id); return ...

Server-side WebSocket doesn't appear to be successfully transmitting messages to the frontend

Using NodeJs with Fastify on the backend in a local environment: Server side: ////// Setting up the app const fastify = require('fastify')({ logger: true }); const path = require('path'); fastify.register(require('@fastify/static& ...

What's causing this javascript to malfunction on the browser?

I encountered a problem with the code in my .js file. Here is a snippet of the code: $.extend(KhanUtil, { // This function takes a number and returns its sign customSign: function(num){ num = parseFloat(num) if (num>=0){return 1} ...

Using jQuery to display checkbox text even when it is not directly adjacent to the checkbox

I'm struggling to display the checked value text as shown in the image without any refresh or click. Can anyone offer assistance? This is my PHP dynamic form: <div class="products-row"> <?php $tq=$conn->query("select * from os_tiffen ...

Personalize the appearance of your stackLabels in Highcharts with dynamic customization options

I recently created a bar graph using Highcharts. You can check it out here: http://jsfiddle.net/v1rbz41q/3/ Here's the code snippet I used: chartw.yAxis [0] .options.stackLabels.formatter = function () {              return "werfdc";   ...

Customizing SVGs for Ion Icons Version 5 in a React Application

I have been using ion icons in React by importing them directly into my index.html. While this method has been working well with the icons from ion icons found here, I know that you can also use custom SVGs by specifying an src attribute in the ion-icon ta ...

the ng-repeat directive disables input controls within the tfoot

When working with JSON data, I encountered a situation where I needed to display different types of student details in a table. For one specific type of student, namely partners, I wanted to include input controls such as checkboxes and buttons. However, e ...

What is the best way to submit updated data from an Angular form?

Currently, I have a situation where multiple forms are connected to a backend service for storing data. My query is whether there exists a typical angular method to identify which properties of the model have been altered and only send those in the POST r ...

Customize the border of the Tab indicator within Material UI

I created a custom NavBar with a unique tab indicator implementation: indicator: { background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)', }, <Tabs classes={{indicator: classes.indicator}} onChange={handleClickTab} value={value}> ...

Adjusting the CSS based on the screen's resolution and size

Currently, I am working with a client who needs interfaces to be adapted for two screens with a resolution of 2560x1600. The challenge is that both screens have the same resolution but different screen diagonals. One has a 27" diagonal and the other... on ...

Troubleshooting Cordova's ng-route functionality issue

I am currently working on an Angular application that includes the following code: // app.js var rippleApp = angular.module('rippleApp', ['ngRoute', 'ngAnimate', 'ngAria', 'ngMaterial']); // configure ou ...

Leveraging the power of axios.all for dynamic operations

Is there a way to efficiently reuse the same Vue-component for both editing and creating new users? While working with vue-router, I have implemented a beforeRouteEnter method to fetch data via API. Depending on whether an ID is set in the URL parameter, ...

Getting the desired value from a CreatableSelect in React can be achieved by following these steps:

I have a form that includes a React library component called React Select. I'm struggling to retrieve the target value of this input field. Here's my code snippet: # CreatableSelect tag <CreatableSelect className={Astyle.selectInput} i ...