简体   繁体   中英

How to switch inputted text between 2 colored boxes in html

So far I only have the code that is able to make 2 boxes, made the 4 buttons, but only 1 button actually does something, and that is the start button where a there is a popup that asks for a name, and after you input that name, it will appear in the first box.

<html>
  <head>
    <script>
      function myTask1() {
        var sentence = prompt("Please enter a name");
        var arrSentence = sentence.split(" ");
        if (arrSentence != null) {
          document.getElementById("answer1").innerHTML = arrSentence.sort(); //so we can use Array.sort() function 
        }
        console.log(sentence);
        return sentence;
      }

      function myFunction() {
        var x = document.getElementById()
      }

    </script>
    <style> </style>
  </head>

  <body>
    <p><button type="button" onclick="myTask1()">Click me!</button></p> 
    <button type="button" onclick="ClearFields();">Clear</button> 
    <button type="button" onclick="myFunction()"> --> </button> 
    <button type="button" onclick="myTask4()"><-- </button> 
      <div clas="box" style="background-color:red; height:200px; margin:20px auto;">
        <center>
          <p id="answer1"></p>
          <center>
        </div>
        <div class="box1" style="background-color:grey; height:200px; margin:20px auto;"> </div>
  </body>
</html>

在此处输入图片说明

I've made some demo code for you. I assume that you're a beginner because the question is basic. This is not a problem though, starting something new is great. You can find the answer on the internet and the best programmers are often people who are good with Google. But I hope by showing you the solution anyway you get a feeling for structure. Try to understand every line. Try write it from scratch afterwards anyway. It's a great exercise.

Code: https://github.com/Bunpasi/stackoverflow-answers/blob/master/js-listbox-selector/index.html

Some things to notice: - I've put the script in the footer so it doesn't interfere with the loading time of the page. - I've put all code in an anonymous function to avoid global functions. - I changed clas to class . - I've used event listeners instead of even attributes. - I didn't duplicate the logic for both boxes but used one function which I can use on both.

After your understand the code, there are some things you can improve on this code. - Make sure the selection doesn't go away after the update. You can store this in the data as well. Right now the data is an array of ID's, but you can turn it into an array of objects containing even more important data, like whether it's selected. - Move the style from the elements to the header.

Don't be discouraged by down votes.

Good luck!

Update

If you want to move all names all the time. This is what you need to do.

This line looks for all selected elements:

var selectedElements = boxes[fromId].querySelectorAll('.list_item.selected');

Remove the selected .selector:

var selectedElements = boxes[fromId].querySelectorAll('.list_item');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM