user7935835
user7935835

Reputation:

I want to remove the <hr> tag from the last <li> tag

What I am trying to make is to remove the last hr tag from my li tag.

JavaScript:

$(document).ready(function() {
  var api = "https://wind-bow.gomix.me/twitch-api";
  var streamers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp",
    "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"
  ];
  var newStreamers = []

  for (var i = 0; i <= streamers.length - 1; i++) {
    newStreamers.push('<li>' + streamers[i] + '</li>' + '<hr>');
  }
  $("#streamers").html(newStreamers.join(""));
  $(".container > .list > ul > li:not(:last-child)").css();
  $(".container > .list > ul > li").css({
    'margin-left': '50px',
    'padding': '30px'
  });
  $("#streamers:last-child hr").css({
    'display': 'none'
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="twitch">
    Twitch
  </div>
  <div class="list">
    <ul>
      <li id="streamers">
      </li>
    </ul>
  </div>
  <footer>
  </footer>
</div>

Here I have the link for my CodePen:

https://codepen.io/Terzio/pen/zjPBoq

Tell me everything is wrong in this code so I can learn from my mistakes and become better.

Upvotes: 0

Views: 2002

Answers (6)

patelarpan
patelarpan

Reputation: 7991

here the your code.

$(document).ready(function() {
  var api = "https://wind-bow.gomix.me/twitch-api";
  var streamers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
  var newStreamers = [];
   for (var i = 0; i <= streamers.length-1; i++) {
    newStreamers.push('<li>' + streamers[i] + '</li>' + '<hr>');
  }
  $("#streamers").html(newStreamers.join(""));
  $(".container > .list > ul > li:not(:last-child)").css();
  $(".container > .list > ul > li").css({'margin-left': '50px', 'padding': '30px'});
  $("#streamers:last-child hr").css({'display': 'none'});
}); 
/* 
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com 
Twitter: @rich_clark
*/

html,
body,
div,
span,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
abbr,
address,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
samp,
small,
strong,
sub,
sup,
var,
b,
i,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  outline: 0;
  font-size: 100%;
  vertical-align: baseline;
  background: transparent;
}

body {
  line-height: 1;
}

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

nav ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: "";
  content: none;
}

a {
  margin: 0;
  padding: 0;
  font-size: 100%;
  vertical-align: baseline;
  background: transparent;
}

/* change colours to suit your needs */
ins {
  background-color: #ff9;
  color: #000;
  text-decoration: none;
}

/* change colours to suit your needs */
mark {
  background-color: #ff9;
  color: #000;
  font-style: italic;
  font-weight: bold;
}

del {
  text-decoration: line-through;
}

abbr[title],
dfn[title] {
  border-bottom: 1px dotted;
  cursor: help;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

/* change border colour to suit your needs */
hr {
  display: block;
  height: 1px;
  border: 0;
  border-top: 1px solid #cccccc;
  margin: 1em 0;
  padding: 0;
}

input,
select {
  vertical-align: middle;
}

/* MY STYLE */
.container {
  margin-top: 20px;
  margin-bottom: 20px;
  height: 100%;
  display: grid;
  grid-gap: 3px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 50px auto 8px;
  grid-template-areas:
    ". twitch ."
    ". list  ."
    ". footer .";
}
.container > div:not(.list) {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32px;
  color: #fff;
  border: none;
}
.twitch {
  grid-area: twitch;
}
.list {
  grid-area: list;
}
footer {
  grid-area: footer;
}
.container > div:nth-child(1n) {
  background-color: #6441a4;
  font-family: "Helvetica", Dimitri;
}
.container > div:nth-child(2n) {
  background-color: #FFE4E1;
}
.container > div:nth-child(2n) > ul {
  list-style-type: none;
  font-family: "Arial Black", Gadget, sans-serif;
  color: #000;
}
.container > footer {
  background-color: #6441a4;
  font-family: "Helvetica", Dimitri;
}
hr {
  border-color: red;
}

#streamers li + hr:last-child  {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="twitch">
    Twitch
  </div>
  <div class="list">
    <ul id="streamers">
    
    </ul>
  </div>
  <footer>

  </footer>
</div>

Upvotes: 0

Rikin
Rikin

Reputation: 5473

You can just use CSS for styling purpose. No need to insert additional dom which is solely used for display purpose. And inserting DOM element between li is not a good practice. Remove <hr> tag and add following CSS in your code, should do it.

.container ul li {
  padding: 20px 15px;
  border-bottom: 1px solid white;
}

.container ul li:last-child {
  border-bottom: none;
}

Upvotes: 0

david mwangi
david mwangi

Reputation: 1

how about you push the li and the hr differently then after the for-loop you pop the last item...

for (var i = 0; i <= streamers.length-1; i++) {
newStreamers.push('<li>' + streamers[i] + '</li>' );
 newStreamers.push('<hr>')

}
newStreamers.pop()

Upvotes: 0

Tomas
Tomas

Reputation: 146

Instead of adding an hr tag that you want to remove, just don't add it.

Replace

newStreamers.push('<li>' + streamers[i] + '</li>' + '<hr>');

with

newStreamers.push('<li>' + streamers[i] + '</li>');

to only push the <li> elements to the array. Then replace

$("#streamers").html(newStreamers.join(""));

with

$("#streamers").html(newStreamers.join("<hr>"));

to glue them together with the <hr> tag.

Upvotes: 0

trevorp
trevorp

Reputation: 1168

You could also do this with just CSS:

#streamers hr:last-child {
  display:none;
} 

And you might want to move your margin from the hr to the li element:

li {
  margin: 1em 0;
}

Upvotes: 0

Ryan Searle
Ryan Searle

Reputation: 1627

You could put the <hr> tag in the join.

$(document).ready(function() {
  var api = "https://wind-bow.gomix.me/twitch-api";
  var streamers = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
  var newStreamers = []

  for (var i = 0; i <= streamers.length-1; i++) {
      newStreamers.push('<li>' + streamers[i] + '</li>'); // Remove <hr> from here
  }
  $("#streamers").html(newStreamers.join("<hr>")); // and add it here
}); 

Upvotes: 2

Related Questions