How to Change H1 using Javascript?

Asked

Viewed 2,436 times

0

I have a product gallery and the side there are some buttons that are the categories of these products (buttons: cabinet, monitors, mouse, chairs, etc), when I click in this category, a toggle active only products of that specific category.

However, I want above these products to contain a title that will initially look like this: "Products" (this is the default), but when I click on the Cabinet category, for example, this title will be changed to "Cabinets". I could tell?

Well, I want that to happen with all the other buttons always changing the title at runtime.

I even tried to create a function by clicking using GetElementById along with a IF ELSE, but it applies the first validation only.

Someone who’s done something like this could help me?

Important: I’m picking up the buttons from the database, simply I did an instruction WHILE that will repeat the buttons according to the number of categories registered in the database, so it does not have so right away, as add a different function for each button. There’s a way, but not so easily.

  • You are using jquery?

  • 3

    Ask the question the code you made to look at what might be wrong.

  • Welcome, important to read this post to get success on your next questions https://answall.com/help/mcve. and to mark an answer as it accepts https://i.stack.Imgur.com/evLUR.png and why it accepts https://pt.meta.stackoverflow.com/questions/1078/comor-e-why-what-to-accept-a-reply/1079#1079

  • Hello @Marcelouchimura! I’m not using jQuery.

  • @Andersoncarloswoss So, since it’s a problem of a client’s website, my manager didn’t allow me to divulge part of the code, but the friends below answered according to the descriptive that I gave and worked, but in future questions, I’ll try to post the code Grateful!!

2 answers

1

See if this is what you need:

<h2 id="meuh2">Produtos</h2>

<p>Nonononononono nonono nonono.</p>

<p>"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. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>

<p>
<select id="s" onchange="esse_mudou(this.value)">
  <option>Produtos</option>
  <option>Gabinetes</option>
  <option>Sabonete líquido</option>
  <option>Ração para cágados</option>
  <option>Buzina de calhambeque</option>
</select>
</p>

<p>
<button onclick="botao_clicado(this)">Produtos</button>
<button onclick="botao_clicado(this)">Gabinetes</button>
<button onclick="botao_clicado(this)">Sabonete líquido</button>
<button onclick="botao_clicado(this)">Ração para cágados</button>
<button onclick="botao_clicado(this)">Buzina de calhambeque</button>
</p>

<script>
    function esse_mudou(valor) {
        muda_h2(valor);
    }

    function botao_clicado(botao) {
        /*faz algo genérico para todos os botões */

        muda_h2(botao.innerHTML);
    }

    function muda_h2(texto) {
        var h2 = document.getElementById('meuh2');
        h2.innerHTML = texto;
    }
</script>

  • So bro, I used your code and gave supercerto also here in my problem, rsrs.. I’ll use Leo’s code up there but thanks for taking the time to help me out. : D

  • vlw flw vlw flw

1


An alternative using Jquery

$(".btn").click(function(){
    var name = $(this).text();
    $("#meuh1").html(name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1 id="meuh1">Produtos</h1>

<button class=btn>Gabinetes</button>

<button class=btn>Processadores</button>

<button class=btn>Discos Rígidos</button>

<button class=btn>Memórias RAM e ROM</button>

text()- returns the text of the element

$(this)

  • $() is the jQuery constructor function.
  • this is a reference to the DOM element invoked.
  • Then in $(this) you’re passing this for the function $() as a parameter, in this case the text of the element text()


Changing the H1 and presenting the corresponding text

$(".btn").click(function(){
    $('div[id^="div_"]').hide(); 
    var name = $(this).text();
    $("#meuh1").html(name);
    var id = $(this).attr('id');
    $("#div_"+id).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1 id="meuh1">Produtos</h1>

<button id="bt1" class=btn>Gabinetes</button>

<button id="bt2" class=btn>Processadores</button>

<button id="bt3" class=btn>Discos Rígidos</button>

<button id="bt4" class=btn>Memórias RAM e ROM</button>


<div id="div_bt1" style="display:none">Gabinetes de várias marcas e modelos</div>
<div id="div_bt2" style="display:none">Processadores de última geração</div>
<div id="div_bt3" style="display:none">Discos Rígidos de até 4MB, rs :)</div>
<div id="div_bt4" style="display:none">Memórias RAM e ROM</div>

  • BOOA! This is exactly what I needed, of course I will need to make some adjustments regarding my code, because, I can not simply, insert a property "id" for each button because I do a While (php) and pass only a <button id="bt1" class=btn>Cabinets</button>, the others are automatically generated by the loop. But that’s another question. jQuery significantly reduced the code I already had to try to solve the problem, it was going to be a little extensive. VLWWW

  • @Lougansdematos, Uai, just put a counter in the loop and concatenate with the id value

  • Uhumm :D vlw man

Browser other questions tagged

You are not signed in. Login or sign up in order to post.