简体   繁体   中英

Storing elements on click with HTML/Javascript?

so Im a noob at html css and javascript, so I this is probably a really obvious question that would be easy to find the answer to with google. Alas, I dont really know where/how to look up what I want, so any tips on finding out how to do specific tasks would be appreciated

Basically, I want to know how to give the users an option to click on a word on a list (or something like a star icon beside the word) so that the word/element can be stored in a "favorites" tab.

Sorry again Im really bad at this.

So basicaly you want to get the text of the element and add it in a array. Like this :

html

<html>
  <head>

  </head>
  <body>
    <div id="scan">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
    </div>
    <span id="needText">tralala</span>
  </body>
</html>

Javascript :

var array = [];
var textSolo = document.getElementById('needText');
var scan = document.getElementById('scan');

textSolo.onclick = function(){getClick(event)};
scan.onclick = function(){getClick(event)}

function getClick(text){
    array.push(text.target.textContent);
}

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