Transferring the default value from one textbox to another

I am in need of assistance. I am attempting to retrieve a default value from one textbox and display it in another using document.getElementById. However, my current code only captures what I have typed, not the original preset value "ROS". Please assist me with this issue.

Take a look at the code snippet below:

<input id="program" name="program1" size="40px" value="ROS" onblur="if(this.value==''){ 
this.value='ROS';}" onfocus="if(this.value=='ROS'){ this.value='';}" onkeyup="Program();">

<input type= "text" id = "program2" readonly size="50px" class="textbox-modal">

<input id="Button1" type="button" value="Preview" class="button" onclick="Program()" />

function Program() 
{
document.getElementById('program2').value = document.getElementById('program').value;
}

Answer №1

Here is a functional solution

const firstInput = document.getElementById('program'),
  secondInput = document.getElementById('program2');
secondInput.value = 'ROS';

function Program() {
  secondInput.value = firstInput.value;
}

function handleBlur() {
  if (!firstInput.value) {
    secondInput.value = firstInput.value = 'ROS';
  }
}
<input id="program" name="program1" size="40px" value="ROS" onblur="handleBlur()" onfocus="if(this.value=='ROS'){ this.value='';};" onkeyup="Program();">

<input type="text" id="program2" readonly size="50px" class="textbox-modal">

<input id="Button1" type="button" value="Preview" class="button" onclick="Program()" />

Additional Information

1. Initially set the value (ROS) for the second input and reassign the value to the second input in the onblur event if the first input is empty.

2. You can also check if the value is empty like this: if (!firstInput.value)
This is equivalent to

if (firstInput.value == '' || firstInput.value == undefined || firstInput.value == null)

Update (Including Numerical Values)

It functions as expected (See the example below).

const firstInput = document.getElementById('day1'),
      secondInput = document.getElementById('day2');

secondInput.value = '0';

function Day1() {
  secondInput.value = firstInput.value;
}

function handleDay1Blur() {
  if (!firstInput.value) {
    secondInput.value = firstInput.value = '0';
  }
}
<input id="day1" size="40px" value="0" onblur="handleDay1Blur()" 
onfocus="if (this.value==0) { this.value=''; };" onkeyup="Day1();">

<input type="text" id="day2" readonly size="40px" class="textbox-modal">
<input id="Button1" type="button" value="Preview" class="button" onclick="Day1()">

Answer №2

I'm back and looking for help with the code snippet above that is meant to handle numeric values. Unfortunately, it keeps returning 0 instead of the expected result. Can anyone provide insight on how I can fix this issue? Below you can find how I've been using the code.

const firstInput = document.getElementById('day1'),
      secondInput = document.getElementById('day2');

secondInput.value = '0';

function Day1() {
  secondInput.value = firstInput.value;
}

function handleDay1Blur() {
  if (!firstInput.value) {
    secondInput.value = firstInput.value = '0';
  }
}

<input id="day1" size="40px" value="0" onblur="handleBlur()" 
onfocus="if (this.value==0) { this.value=''; };" onkeyup="Day1();">

<input type="text" id="day2" readonly size="40px" class="textbox-modal">
<input id="Button1" type="button" value="Preview" class="button" onclick="Day1()">

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

Maintain the cache in internet browsers

Despite my efforts to prevent browser caching, the URLs of my applications are still being stored in cache. I have implemented the following code in the page load method of my master page to disable browser cache: Response.Cache.SetCacheability(HttpCachea ...

Sign up for a new magazine or newsletter once your current one has been fully processed | React-Meteor

Currently, I am working with two publications: teams.dashboard and boards.board. My goal is to load the boards.board publication using the boardId from the first loaded Board, which can be accessed like this: Boards.find().fetch()[0]._id. I'm searchi ...

What is the best way to toggle the visibility of a side navigation panel using AngularJS?

For my project, I utilized ng-include to insert HTML content. Within the included HTML, there is a side navigation panel that I only want to display in one specific HTML file and not in another. How can I achieve this? This is what I included: <div ng ...

In HTML, adjust column widths for multiple tables according to the widest column present in any of them

With Python, I am creating an HTML page that contains multiple tables with the same number of columns, all holding the same type of data. While the generated page is functional, I would like to improve readability by ensuring that all tables have the same ...

Exploring the possibilities of Three.JS by manipulating the world position of a child 3D object

I have a child object3D that is part of a group Object3D. While the child object's position is displayed relative to the parent object's space, I am trying to change the location of the child object within the 3D space. To do this, I first need t ...

Why is the jQuery datepicker malfunctioning when nested within ng-repeat in AngularJS?

I am currently facing an issue with the jquery ui date picker in my AngularJS application. The date picker is functioning correctly outside of any ng-repeat loops, but it stops working when placed within one. <input type="text" class="form-control date ...

Issue with adding child elements within a loop

I need assistance with appending an existing div (stored in the variable "parent") to a new div multiple times within my code. Below is the snippet of code I am working with. Currently, it appends the existing div to the last created div. Can someone hel ...

The Webstorm AngularJS extension appears to be malfunctioning, as it is not able to recognize the keyword 'angular'

As a beginner in Angularjs and web development, I have been using Webstorm to develop my projects. I have already installed the Angularjs plugin, which seems to be working fine in my HTML files. However, I am facing an issue with my .js file. In this file, ...

Is it possible to create dynamic backgrounds using Twitter Bootstrap's responsiveness?

Is there a way to create a responsive image arrangement for backgrounds on a website? Imagine having different background images load based on various screen resolutions. Additionally, it would be advantageous to specify different behaviors such as having ...

Tips for implementing a countdown timer with JavaScript

I have created a program using HTML that functions like a website but works offline. I am looking to incorporate a 35-minute countdown timer that will automatically log out the user once the time is up. However, my current JavaScript code only counts down ...

unexpected alteration of text sizing in mathjax within reveal.js presentations

Something strange is happening with the font size in my slides. The code for each slide is the same, but there is an unexpected change between the 3rd and 4th slide. I cannot figure out what is causing this discrepancy. Oddly enough, when I remove the tit ...

Error encountered: Scrollanimation IOS syntax error- Unexpected token '=.' an open parenthesis '(' was expected before a method's parameter list

Encountering an issue with the scroll animation on older IOS devices (2019 and older) - I receive the following error message: SyntaxError: Unexpected token '='. Expected an opening '(' before a method's parameter list. class Event ...

When using jQuery to select elements of a specific class, make sure to exclude the element that triggered the

A dynamic number of divs are generated from a data source. Each div contains an image button and another div with text. While the actual scenario is more complex, we can present a simplified version: <div id="main"> <div id="content_block_1" ...

The navigator's userAgent property is used to match specific handset identifications

When identifying users on specific devices, I use navigator.userAgent.match to detect certain phones. However, with Android devices, there are various tablets, phones, and set-top boxes to consider. So, my code snippet looks like this: if(navigator.userAg ...

Develop a unique splitter code that utilizes pure javascript and css, allowing users to drag and drop with precision

I am facing an issue with setting the cursor above my drop panel, as it results in a flicker effect. How can I set the cursor for my example to work properly? I have tried multiple different approaches to make this work. Although I am using the provided ...

Any ideas on how to potentially establish a default route based on conditions with react-router-dom v6?

Managing two pages, Page1 and Page2, requires conditional configuration for setting one as the homepage based on a specific condition. Additionally, all URLs must be prefixed with an ID. Take a look at the code snippet below: <Routes> <Route pat ...

"Oops! Looks like there's a reference error - the function you're trying

Javascript: function insertSmiley(a) { var $img = $(a).children('img'); $("#message").insertAtCursor(($("#message").data("wbb").options.bbmode) ? $("#message").data("wbb").toBB($(a)): $(a).html()); return false; } HTML: <a href= ...

I found that using Enzyme's shallow().text() in React Native did not perform as I had anticipated

I'm currently exploring React Native testing using enzyme and react-native-mock. Excluded from the discussion: A customized compiler for mocha to utilize Babel features. Here is the code snippet: Block.jsx: import React from 'react'; imp ...

The code functions properly on React Webpack when running on localhost, however, it fails to work once deployed to AWS Amplify

As I work on my React.js app, I encountered an issue with hosting pdf files on Amplify. While everything runs smoothly on localhost/3000, allowing me to access and view the pdf files as desired either in a new tab or embedded with html, the same cannot be ...

Adjust the marginLeft and marginRight values in a JavaScript statement within a Highcharts configuration

Is there a way to adjust the chart marginLeft and marginRight using Highcharts, and then redraw it in JavaScript? I am looking to change the chart margin dynamically at different points in my code. http://jsfiddle.net/ovh9dwqc/ I attempted to do this wit ...