David Cienfuegos
David Cienfuegos

Reputation: 1

Add button after a div with script on wordpress

I need to append a button after one div in my wordpress, i try this snippet but its not working, any idea how should it be? Thanks.

<script>
  jQuery(document).ready(function(){
    var posicion = document.getElementsByClassName("sf-field-search");
    var boton = document.createElement("button");
    boton.innerHTML = "Ver/Ocultar filtros";
    posicion.appendChild(boton);
  });
</script>

Upvotes: 0

Views: 295

Answers (1)

rawnewdlz
rawnewdlz

Reputation: 1265

If you're using jQuery it can be done like this. I included a class on the button to style it.

jQuery(document).ready(function() {
  var posicion = $('.sf-field-search');
  posicion.append('<button class="test">Ver/Ocultar filtros</button>');
});
.test {
  display: block;
  padding: 5px;
  background: blue;
  color: white;
}

.test:hover {
  background: cyan;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sf-field-search">sf-field-search</div>

Upvotes: 2

Related Questions