Categories
Development HTML jQuery

How to create a proper click event in js, jquery, html

So as an assignment I need to create a barebones twitter clone. I was given a script that creates random tweets from four users and to display them I’m simply using an interval. My issue is that one of the condition requires that if the user clicks on one of the usernames, the website should show that users tweet history. The tweets are all stored inside the generator file. I have literally been trying for over a day to get a dynamic click script to run. I every time a tweet gets appended to the website it has a class of the users name. The closest I have gotten in terms of logic was to put an onclick=functionName(userclass) inside the anchor of my username. But it is out of scope. I’m pretty new at html/jquery. In fact it is my first week so I have been reading up for hours and you all know how it is when you start learning this stuff. The more you read, the more questions you have. Hope somebody can shine some light

Here’s the code:

<!DOCTYPE html> <html>   <head>

    <title>TWIDDLER</title>
    <link rel="stylesheet" href="style.css">
    <script src="jquery.js"></script>
    <script src="data_generator.js"></script>
    <script src="history.js"></script>   </head>

  <body>
    <img class="bird" src="icon.png">
    <h1>
      <span class='title' id="top">TWIDDLER (def not Twitter)</span>
    </h1>

    <script>
      $(document).ready(function(){
        var $body = $('.tweets');
        var index = 0;
        setInterval(function(){
          var tweet = streams.home[index];
          var $tweet = $('<div></div>');
          var hypername = tweet.user; 
          var $username = $("<a onclick='gethistory("+hypername+")' href='#top' class='"+hypername+"'><span></span></a>");
          var $endline = $('<br>');
          console.log($username);
          var time = tweet.created_at.toLocaleTimeString();
          $tweet.text(tweet.message + ' | ' + tweet.created_at.toLocaleTimeString());
          $username.text('@' + tweet.user + ': ');
          $username.appendTo($body)
          $tweet.appendTo($body);
          // console.log($tweet);
          // var testname = $('<h6></h6>').text('***'+hypername+'***').appendTo($body);
          $endline.appendTo($body);
          index += 1;
        }, 2000);
         var gethistory = function(name) {
           console.log("I clicked it");
          for (var i = 0; i < streams.users[name].length; i++) {
            var tweethist = streams.users[name][i];
            var $tweethist = $('<div></div>');
            var $endline = $('<br>');
            $tweethist.text(tweet.message + ' | ' + tweet.created_at.toLocaleTimeString());
            $tweethist.appendTo('#history');
            $endline.appendTo('#history');
          }
        };
      });
    </script>

    <h3>
    <section class="row">

        <div class="column1 tweets left"><h2>Tweeds</h2></div>

        <div class="column2 right" id="history"></div>

    </section>
    </h3>

    <!-- <section>
      <audio controls autoplay src="song.mp3"></audio>
    </section> -->

  </body>

</html>

Leave a Reply

Your email address will not be published. Required fields are marked *