mardi 22 octobre 2019

Display Rnadom Content From Object

I saw in a project a function that promoted random values from different objects in javaScript and display content inside HTML tag,I want to make something like that but different. When I a button to take random value to a single object and display it inside a div.

I create some object in JS and a action when click a button display object but I don't know how to put all content inside a div and to take random information.

function deosebitProject() {
  var site_1 = {
    name: "Site 1",
    url: "https://www.google.ro/",
    description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
    site_image: ["https://adpconstruct.ro/wp-content/uploads/2019/07/poza52.jpg", "https://adpconstruct.ro/wp-content/uploads/2019/07/poza52.jpg", "https://adpconstruct.ro/wp-content/uploads/2019/07/poza52.jpg"]
  }

  var site_2 = {
    name: "Site 2",
    url: "https://www.google.ro/",
    description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
    site_image: ["https://adpconstruct.ro/wp-content/uploads/2019/07/poza52.jpg", "https://adpconstruct.ro/wp-content/uploads/2019/07/poza52.jpg", "https://adpconstruct.ro/wp-content/uploads/2019/07/poza52.jpg"]
  }

  document.write(
    '<div class="container">',
    '<div class="row">',
    '<div class="col-md-6">',
    '<h2>' + site_1.name + '</h2>',
    '<a href="' + site_1.url + '">' + site_1.url + '</a>',
    '<p>' + site_1.description + '</p>',
    '</div>',
    '<div class="col-md-6">',
    '<img src="' + site_1.site_image[0] + '"><br /> <img src =' + site_1.site_image[1] + '"><br /> <img src = "' + site_1.site_image[2] + '">',
    '</div>',
    '</div>',
    '</div>'
  );

  document.write(
    '<div class="container">',
    '<div class="row">',
    '<div class="col-md-6">',
    '<h2>' + site_2.name + '</h2>',
    '<a href="' + site_2.url + '">' + site_2.url + '</a>',
    '<p>' + site_2.description + '</p>',
    '</div>',
    '<div class="col-md-6">',
    '<img src="' + site_2.site_image[0] + '"><br /> <img src =' + site_2.site_image[1] + '"><br /> <img src = "' + site_2.site_image[2] + '">',
    '</div>',
    '</div>',
    '</div>'
  );
}
<!doctype html>
<html>

<head>
  <title>Javascript Challange 1</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="random.js"></script>
</head>

<body>
  <div>
    <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce blandit, erat ac lobortis tristique, eros eros tristique enim, at imperdiet lectus orci fermentum ex. Suspendisse at nisl pretium, congue nunc sit amet, rutrum lectus. Integer eu luctus
      metus. Duis placerat finibus urna eu euismod. Vestibulum mollis viverra lacinia. Curabitur odio elit, venenatis eget congue sit amet, sagittis bibendum risus. Donec pretium id justo nec elementum. Duis interdum non ligula at imperdiet. Nunc ultricies
      ac arcu id aliquet. Ut vitae risus magna. Curabitur mollis placerat tortor, nec finibus mi elementum nec. Pellentesque vitae tortor elementum, porttitor magna sit amet, dapibus nisl. Nullam sed dolor id enim tempus porttitor. Curabitur eleifend
      condimentum nisl, id placerat purus semper sed. Nulla facilisi. Donec vehicula tempor nulla a tempor. Nulla quis rhoncus elit. Aenean sagittis auctor turpis vel pharetra. Donec est nunc, molestie sit amet ullamcorper sed, facilisis nec sapien. Mauris
      mauris quam, porta sed ante ut, egestas ultricies augue. Pellentesque ornare arcu ac vestibulum mattis. Sed nec malesuada turpis. Cras aliquam turpis nec justo tempus finibus. Morbi et luctus est.Curabitur eget malesuada neque. Lorem ipsum dolor
      sit amet, consectetur adipiscing elit. Maecenas sed sagittis arcu. Maecenas quis orci quis lorem luctus feugiat placerat in tellus.
    </p>
  </div>
  <div id="our-project">
    <button onclick="deosebitProject()">View Project</button>
  </div>
</body>

</html>



Aucun commentaire:

Enregistrer un commentaire