Relocating the output of JavaScript to a different location

I'm currently facing an issue with transferring my JavaScript output (start) from the black box to the grey box. I am unsure about what needs to be included in the functions (moveRight and moveLeft) for the transition to occur smoothly.

Even after researching for a solution, I couldn't find one that fits my requirements. I believe there is a more elegant way to showcase this code, but as I am still in the process of learning the basics, it seems challenging. Any assistance would be greatly appreciated. Thank you!

function start() {
  var txt;
  var person = prompt("Please enter your name:", "");
  if (person == null || person == "") {
    txt = "User cancelled the prompt.";
  } else {
    txt = person;
  }
  document.getElementById("text").innerHTML = txt;
}

function moveRight(start) {

}

function moveLeft(start) {

}
.blackbox {
  width: 250px;
  height: 125px;
  background: #000000;
  float: left;
  color: white;
  text-align: center;
  height: 90px;
  line-height: 90px;
}

.greybox {
  width: 250px;
  height: 125px;
  background: #323232;
  float: left;
  text-align: center;
  height: 90px;
  line-height: 90px;
}

.button {
  position: relative;
  float: left;
  padding-left: 10px;
}

.part3 {
  position: relative;
  float: left;
}
<div id="part3">
  <button type="button" onclick="start()">Start</button>
  <button type="reset">Clear</button><br><br><br>

  <div class="part3">
    <div class="blackbox" id="text"></div>
    <div class="button">
      <br>
      <button type="button" onclick="moveRight(start)">--></button><br><br>
      <button type="button" onclick="moveLeft(start)"><--</button>
    </div>
    <br><br>
    <div class="greybox" id="name"></div>
  </div>
</div>

Answer №1

This piece of code allows the buttons to switch the content between the black and gray boxes

var message;

function getUserName() {
    var person = prompt("Please enter your name:", "");
    if (person == null || person == "") {
        message = "User cancelled the prompt.";
    } else {
        message = person;
    }
    document.getElementById("text").innerHTML = message;
}

function moveTextRight() {
    document.getElementById('name').innerHTML = message;
    document.getElementById('text').innerHTML = '';
}

function moveTextLeft() {
    document.getElementById('text').innerHTML = message;
    document.getElementById('name').innerHTML = '';
}
.blackbox{
    width:250px;
    height:125px;
    background:#000000;
    float: left;
    color: white;
    text-align: center;
    height: 90px;
    line-height: 90px;
}

.greybox{
    width:250px;
    height:125px;
    background:#323232;
    float: left;
    text-align: center;
    height: 90px;
    line-height: 90px;
}

.button{
    position: relative;
    float: left;
    padding-left: 10px;
}

.part3{
    position: relative;
    float: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<meta charset="UTF-8">
</head>
<body>

<div id="part3">
<button type="button" onclick="getUserName()">Start</button>
<button type="reset">Clear</button><br><br><br>

<div class="part3">
<div class="blackbox" id="text"></div>
<div class="button">
    <br>
    <button type="button" onclick="moveTextRight()" >--></button><br><br>
    <button type="button" onclick="moveTextLeft()" ><--</button>
</div>
<br><br>
<div class="greybox" id="name"></div>
</div>
</div>

</body>
</html>

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

Trick for hiding CSS elements when input field is vacant

Is there a way to hide the search result when the input is empty? While everything is functioning correctly, I would like to prevent the search result from remaining visible after clearing the input field. For example, if you type 'A' and then d ...

Extracting data from a text editor using React.js

Currently, I am using a Quill JS component, which is a text editor designed for React JS. I am thoroughly testing its features and functionalities. I have successfully created an editor with a toolbar. However, I am facing a dilemma. I am unsure of how to ...

Having trouble connecting the controller variable in AngularJS to the HTML page

I'm struggling with a simple query that doesn't seem to be working for me. When making an Ajax call and retrieving data from the backend, everything seems to be in order. Here's the code snippet: $.ajax({ type: "GET", url: service ...

Step-by-step guide on creating a linear graph in CSS using a table

I am a newcomer to the world of CSS and I am looking to create a graph using CSS without the use of any libraries or frameworks. Can someone kindly assist me in drawing a linear graph using this HTML table? Appreciate any help in advance. <html> & ...

After I designed a single modal for all buttons, I encountered an infinite loop error

I'm facing an issue with my buttons and modal functionality. I have three buttons with different content, each linked to a modal that should display the corresponding data upon click. However, after reloading the page, I encounter an infinite loop in ...

Display issues with CSS Absolute Positioning in IE11

I have encountered an issue with my html and css code that uses absolute positioning. While it functions correctly on Chrome and Firefox, I am facing a problem with Internet Explorer. The absolute position property in the SVG class does not seem to work in ...

Is the ng-style not displaying the background image with the interpolated value?

I have been attempting to update the background image of a div using angular ng-style. Below is the code I am working with: <div class="cover-image" ng-style="{'background-image' : 'url({{data.image}})'}"></div> However, ...

Recursive sorting and parsing of JSON data with multiple levels

I'm new to using recursion in JavaScript and need some guidance to understand it better. I have a JSON data structure with multiple levels of nested "subcategories". const STORE_CATEGORIES = [{ "Id":"1", "Name":"One Parent", ...

Is it possible to create an index.html page with all the necessary head tags to prevent duplicate code across multiple html pages?

Imagine I am using app.js or a Bootstrap CDN link for all of my HTML pages, but I don't want to include it in every single page individually. Is there a way to create an index.html file that includes this so that all other HTML pages load the head fro ...

Executing javascript code within the success function of the $ajax method in jQuery: A step-by-step guide

The code snippet below includes a comment before the actual code that is not running as expected. $(document).on('click', '#disable_url', function (e) { e.preventDefault(); var items = new Array(); $("input:checked:no ...

Utilizing JavaScript's Array.sort() method for sorting objects with varying properties

Currently, I am dealing with these specific Objects: let obj1 = { from: Date, to: Date } let obj2 = { date: Date } These Objects have been placed in an Array: let arr = [ obj1, obj2 ] My goal is to organize the objects within the array by using arr.sort( ...

Encountered a problem while attempting to convert column chart to 3D using Google Sheets API

I've encountered a problem while attempting to convert all column charts in my spreadsheet to 3D column charts. Despite following Google's documentation, I receive an error message when running the code below. Could this be an issue with the API ...

Using CSS, the ability to scroll to the top of a page is restricted when an inner div expands to the top while using flex

While following a tutorial on YouTube, I encountered an issue where my expanding content, when exceeding the size of the window in a small screen view, prevented me from scrolling to the top. Interestingly, I could scroll to the bottom and back up to that ...

The npm package for @azure/storage-blob doesn't seem to play nice with the Azure Blob storage modules for IoT Edge

Currently, I am developing an edge module using Node.js to interact with Azure Blob storage modules on IoT Edge platform. To achieve this, I am following the documentation available at https://www.npmjs.com/package/@azure/storage-blob. The npm package ut ...

Editing text on an Android device - Removing formatting pieces

I've encountered an issue where trying to remove spans from an EditText using EditText.getText().clearSpans() results in strange behavior. Line feeds start appearing as boxes and any newly set spans are misplaced. My question is: Is there a way to cl ...

Error Encountered in Jhipster Application During Initial Launch

Struggling with running a newly generated Jshipster 3 application using gulp serve after successful generation. My setup includes Ubuntu 16.04 and npm 3.9, but I keep encountering an error when attempting to execute gulp serve: check out this screenshot ...

Is it possible to change the input type of 'time' to a string when utilizing ng-change in AngularJS?

Is there a way to convert a time input into a string or a timestamp for Firebase support? For instance, the code below will not function correctly due to the time input type. HTML <html ng-app='app'> <head> <script src="http ...

Web Audio API functions are encountering a playback issue on iOS 13.3, despite working smoothly on previous iOS versions

I have been developing an audio visualizer using the web audio API. It was functioning smoothly on PC, however, after upgrading to iOS 13.3, it no longer operates on Apple mobile devices. The root cause of this issue remains a mystery to me. The problem s ...

What is the reason behind the TypeError thrown when attempting to assign to `NaN` or `undefined` in JavaScript

A.S.: The question pertains to the type of error rather than the phenomenon itself "use strict" results in a TypeError when system variables like NaN and undefined are modified. But why is it categorized as a TypeError instead of a SyntaxError? Edit: I ...

The sendKeys() method is malfunctioning in Selenium WebDriver

When attempting to enter phone numbers into the designated field, an error is being encountered. Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element The following code snippet is involved: driver.get("https://m ...