-2

i've created a simple banner that sits above my navigation. And i want to give my users the chance to close this banner any time.

That's the banner right now:

enter image description here

     <div class="header-top discord">
      <div class="container-fluid">
        <div class="discordbanner">
          <p class="pull-left">Join our <img style="width:125px" src="assets\images\random\discord.svg"/> Server today and connect with other...!</p>
          <a href="#" class="btn btn-sm btn-yellow pull-right">Join our Discord!</a>
        </div>
      </div>
    </div>

And i want to have something like a simple button with an X to indicate that this element can be closed.

Thats how it should look like:

enter image description here

So basically i want to delete the ID header-top discord and its content with a simple close button. Via JS or jquery doesn't really matter.

Thank you!

Junius L
  • 15,881
  • 6
  • 52
  • 96
Leeooh
  • 11
  • 1
  • 2
  • Welcome to Stack Overflow! An important part of the questions here are showing your attempts at resolving them yourself. What have you tried? – zfrisch Apr 27 '17 at 21:49
  • 1
    Would be nice if there were a remove method. – Kevin B Apr 27 '17 at 21:49
  • Possible duplicate of [Remove all elements of a certain class with JavaScript](https://stackoverflow.com/questions/10842471/remove-all-elements-of-a-certain-class-with-javascript) – leonheess Oct 21 '19 at 13:54

3 Answers3

1

JQuery: $(".header-top discord").remove();

You can call this on a button click

<script>
$("button").click(function() {
  $(".header-top discord").remove();
});
</script>

Javascript:

var node = document.getElementsByClassName("header-top discord")[0];
if (node.firstChild) {
  node.parentNode.removeChild(node);
}

References: MDN - removeChild
Remove all elements of a certain class with JavaScript

Community
  • 1
  • 1
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
1

HTML:

<button class="btn">X</button>

Pure JS:

var btn = document.querySelector('button');
var headerClass = document.querySelector('.header-top discord');

btn.addEventListener('click', function() {
   headerClass.style.display="none";
});

Or jQuery:

$('button').on('click', function() {
   $('.header-top discord').hide(); //puts display: none; to styles
});

But I prefer .fadeOut('slow'); for these occasions.

tholo
  • 511
  • 3
  • 8
  • 19
0

This code can be used to hide the banner:

$('.header-top discord').hide();