How to create function with SINGLE BUTTON to open several class together?

0

In the current function below, it opens an ID (title) on each click.

And you can open several if you click each.

It's great and working perfect (I think)

But I need to have a single separate button that opens all together and close all.

(and keep the current open / close function each)

I thought about using the class, which is common in all titles, but I have been researching for days, I have tried several examples and I have not been able to. Help an old apprentice avoid a stroke.

Thank you all for the help.

ps: Can I further reduce functions? I think it's redundant.

<script src="/1.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){ //aqui troca a cor do titulo
$('.cor').click(function(){
$trClass = $(this).attr('class');
	if ($trClass == undefined || $trClass == 'desclicado'){
	$(this).attr('class', 'clicado');
	}else{
		$(this).attr('class', 'desclicado');
        }
	});
});
function abre(ab){ //aqui abre e fecha cada uma
if	(document.getElementById(ab).style.display=='block')
	{document.getElementById(ab).style.display='none';}
else{document.getElementById(ab).style.display='block';}
}
</script>

PHP/HTML5
$consulta=$pdo->query("select titulo,texto from...
while...
<div class="alinha" onclick="abre('texto<?=$id?>')"><span class="cor"><b><?=$titulo?></b></span>
	<span id="texto<?=$id?>" style="display:none">
	<div>TEXTO</div>
	</span>
</div>	
    
asked by anonymous 27.10.2017 / 13:25

4 answers

2

You could make a single button that controls the open / close state, but in my humble opinion, the state control for a button that opens and closes all elements is somewhat confusing, or for the developer or user. Well, time you can have all the elements open and the marked state is closed, and you click the button and did the opposite of what you expected.

In this case, I suggest creating two buttons with specific functions, one to open all and the other to close all.

The first step is to define HTML. You will need to create both buttons and set class to span you want to show / hide. It is not mandatory to set class , but it will make it easier.

$(document).ready(function(){
    $('#exibir-todos').click(function(){
        $('.alinha .texto').show();
    });
    $('#esconder-todos').click(function(){
        $('.alinha .texto').hide();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script><buttontype="button" id="exibir-todos" >exibir todos</button>
<button type="button" id="esconder-todos" >esconder todos</button>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

JavaScript was written using jQuery 1.3.1, as in its example.

The same example is also available in codepen.io using the most current jQuery.

Update

In the comments, the difference between jQuery 1.3.1 and 3.2.1 and the use of a whole library to use only a single function was questioned.

About the differences between the versions of the library, can be seen in the links below:

What is the difference with jquery version 1, version 2 and version 3 versions release?

jQuery 3.0: The Next Generations

Briefly (free translation of the first link):

  • jQuery 1: the first stable release;
  • jQuery 2: Removed IE 6-8 support for performance gain and library size reduction;
  • jQuery 3: Support Promises / A + for Deferreds, $.ajax and $.when . % compliant with HTML5.

What should be kept in mind is that jQuery has as its main purpose to normalize the differences and provide compatibility between browsers and between different versions of the same browser. Next, it is to provide functions that facilitate the work of a developer and reduce the development code. Facilitation which can be compared to syntactic sugar (although it is not this is the case).

However, I agree with the size generated by the library for something so simple. The example was only developed with jQuery, because in the question example it was already used. Which, in the end, left the response / function "leaner."

In this case, your problem can also be solved with pure javascript, with a bit more code:

function addListener(elem, type, fn) {
    if (elem.addEventListener) {
        elem.addEventListener(type, fn, false);

    } else if (elem.attachEvent) {
        elem.attachEvent("on" + type, function() {
            return fn.call(elem, window.event);
        });
    } else {
        elem["on" + type] = fn;
    }
}

var exibirTodos = document.getElementById('exibir-todos');

var callbackExibirTodos = function() {
  var elements = document.getElementsByClassName('texto');
  
  for(var i = 0; i < elements.length ; i++)
  {
    elements[i].style.display='inline';
  }
};

addListener(exibirTodos , 'click' , callbackExibirTodos);

var esconderTodos = document.getElementById('esconder-todos');

var callbackEsconderTodos = function() {
  var elements = document.getElementsByClassName('texto');
  
  for(var i = 0; i < elements.length ; i++)
  {
    elements[i].style.display='none';
  }
};
addListener(esconderTodos , 'click' , callbackEsconderTodos);
<button type="button" id="exibir-todos" >exibir todos</button>
<button type="button" id="esconder-todos" >esconder todos</button>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

<div class="alinha">
  <span class="cor">cor</span>  
  <span class="texto">texto</span>  
</div>

To improve the compatibility of the .data() function, the Stack Overflow developed function was used

What was created, in the final response, was pretty much the same thing that was developed using jQuery. Add an event to each button with its addEventListener (display / hide).

    
27.10.2017 / 13:52
1

You can use the slideToggle and slideDown property of Jquery

$( "button" ).click(function() {
  if(!$(this).hasClass('todos')){
    $( this ).prev('.cor').slideToggle( "fast" );
  }else{
    $('.cor').slideDown( "fast" );
  }
});
.ctn {
  display: flex;
}
.cor {
  height: 58px;
  width: 58px;
  display: none;
}
.verde{
  background: green;
}
.amarelo{
  background: yellow;
}
.azul{
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="ctn">
  <div class="items">
     <div class="verde cor">
       
     </div>
     <button>
       Ver cor
     </button>
  </div>
  <div class="items">
     <div class="azul cor">
       
     </div>
     <button>
       Ver cor
     </button>
  </div>
  <div class="items">
     <div class="amarelo cor">
       
     </div>
     <button>
       Ver cor
     </button>
  </div>
  <div class="items">
     <button class="todos">
       Ver todos
     </button>
  </div>
</div>

If you do not want the transition pass 0 as a parameter.

    
27.10.2017 / 13:49
1

You can put a class in the tags <span/>

<div class="alinha" onclick="abre('texto<?=$id?>')"><span class="cor"><b><?=$titulo?></b></span>
    <span id="texto<?=$id?>" class="alinha-texto" style="display:none">
        <div>TEXTO</div>
    </span>
</div>

With this class you can change the display property of all alinha-texto elements using the following function

function abreOuFechaTextos() {
    var textos = document.getElementsByClassName('alinha-texto');
    Array.prototype.forEach.call(textos, (el) => el.style.display = (el.style.display === 'none') ? 'block' : 'none');
}

Here's an example:

    function abreOuFechaTextos() {
        var textos = document.getElementsByClassName('alinha-texto');
        Array.prototype.forEach.call(textos, (el) => el.style.display = (el.style.display === 'none') ? 'block' : 'none');
    }
<button onclick="abreOuFechaTextos()">Botão</button>

<div class="alinha" onclick="abre(texto-1)"><span class="cor"><b><?=$titulo?></b></span>
    <span id="texto-1" class="alinha-texto" style="display:none">
        <div>TEXTO</div>
    </span>
</div>


<div class="alinha" onclick="abre(texto-2)"><span class="cor"><b><?=$titulo?></b></span>
    <span id="texto-2" class="alinha-texto" style="display:none">
        <div>TEXTO 2</div>
    </span>
</div>


<div class="alinha" onclick="abre(texto-3)"><span class="cor"><b><?=$titulo?></b></span>
    <span id="texto-3" class="alinha-texto" style="display:none">
        <div>TEXTO 3</div>
    </span>
</div>


<div class="alinha" onclick="abre(texto-4)"><span class="cor"><b><?=$titulo?></b></span>
    <span id="texto-4" class="alinha-texto" style="display:none">
        <div>TEXTO 4</div>
    </span>
</div>

<div class="alinha" onclick="abre(texto-5)"><span class="cor"><b><?=$titulo?></b></span>
    <span id="texto-5" class="alinha-texto" style="display:none">
        <div>TEXTO 5</div>
    </span>
</div>
    
27.10.2017 / 13:53
0

André and Gabriel: Their guidelines were great and functional and helped me a lot. Thanks for the help and I consider myself satisfied.

Gabriel, your attention and patience are commendable. I'm studying JQuery and found countless features that help a lot.

And thanks to stackoverflow we can extend our knowledge

Thank you all.

    
29.10.2017 / 05:03