How to disable and enable the . Click() function of a span when I click on another span using on() off()

Asked

Viewed 886 times

0

I have two spans that work as buttons, when clicking on them appears an input to put an email and if you click again hides these inputs.

I managed to do the following:

Click on span1 -> Input1 appears and disables span2, and if you click it again hides input1 but does not enable span2

And I would like to click on it again besides hiding the input1 it enables the span2, a kind of toggle() in the off();

Follows the JS:

$('.new-step-email-aluno').hide();
		$('#btn-aluno').on('click', function() {
			$('.new-step-email-aluno').toggle();
			$('#btn-familiar').off();
		});

		$('.new-step-email-familiar').hide();
		$('#btn-familiar').on('click', function() {
			$('.new-step-email-familiar').toggle();
			$('#btn-aluno').off();
		});

inserir a descrição da imagem aqui

I don’t know if it’s clear, I’ll fix anything. vlw

1 answer

1

Follows a solution

$(function() {

  var $buttons = $('#btn-aluno,#btn-familiar');
  var $input = $('.new-step-email-aluno, .new-step-email-familiar');

  $buttons.on('click', function(e) {

    var $self = $(this);
    var active = $self.is('.active');
    var aluno = $self.is('#btn-aluno');
    var already = $('#btn-aluno.active , #btn-familiar.active').size() > 0;
    //Desabilita o click se houver um ativo.
    if (!active && already)
      return;

    if (!active) {

      if (aluno) {
        $('.new-step-email-aluno').show();
        $('.new-step-email-familiar').hide();
      } else {
        $('.new-step-email-aluno').hide();
        $('.new-step-email-familiar').show();

      }
    } else
      $input.hide();

    $buttons.removeClass('active');

    if (!active)
      $self.addClass('active');

  });

});
form { text-align:center;}
#btn-aluno , #btn-familiar { cursor: pointer; }
.new-step-email-familiar , .new-step-email-aluno { display:none;}
.active { border: 2px solid #333 !important; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/flat-ui/2.2.2/css/flat-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="btn-aluno" class="btn btn-primary">Sou aluno</span><br> <input type="text" name="fname" class="new-step-email-aluno"><br>
<span id="btn-familiar" class="btn btn-primary">Sou familiar</span><br>
<input type="text" name="fname" class="new-step-email-familiar" ><br>

  • I tried to adapt to my code and it didn’t work, remembering that I use spans and not pq Buttons I have more button on the same page and I have more inputs too. I tried to use an if to see if the input is with display None if it is not disabled and if you have it enabled, but it did not work out tb.

  • just replace the snippets var $buttons = $('button'); for var $buttons = $('#btn-aluno,#btn-familiar'); and var $input = $('input'); for var $input = $('.new-step-email-familiar'); besides var aluno = $self.is('#aluno'); for var aluno = $self.is('#btn-aluno'); and var already = $('button.active').size() > 0; for var already = $('#btn-aluno.active,#btn-familiar.active').size() > 0;

  • Making these Replaces my code will work in your html.

  • Dude I managed to solve, it was simple, I don’t know if I can edit my post with the help link q solve the problem.

  • 1

    I updated the code for you with the solution since Voce was having difficulty putting in your html

Browser other questions tagged

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