olios
olios

Reputation: 9

The button carries out a double command

I need help because I am making a button that turns the div on and off. I noticed that when I ran console.log ('text'), I was getting the value of text twice. I don't know if it's something with my HTML or CSS but when I made a simple button everything worked. I am enclosing the code below and please help.

Script:

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 36px;
  width: 36px;
  left: 5px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  -webkit-transform: translateX(54px);
  -ms-transform: translateX(54px);
  transform: translateX(54px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
  $(document).ready(function() {
    $('.switch.switch1').click(function() {
      console.log('text')
    });
  });
</script>



<div style="display: flex; justify-content: space-around; text-align: center; height: 100px;">
  <h3 style="margin-top: 10px">Random text
    <h3>
      <label class="switch switch1">
            <input type="checkbox">
            <span class="slider round"></span>
        </label>
</div>
<div class="section1" style="margin-bottom: 50px; display: none;">
  <h3 style="text-align: center; margin-bottom: 10px;">Random text</h3>
  <div style="display: flex; justify-content: space-around;">
    <textarea style="width: 35%; height: 250px;"></textarea>
  </div>
</div>

Console image here

Upvotes: 0

Views: 36

Answers (1)

0stone0
0stone0

Reputation: 44073

Use

$('.switch.switch1').change(function() {

.change() docs

Instead off

$('.switch.switch1').click(function() {

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 36px;
  width: 36px;
  left: 5px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked+.slider {
  background-color: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  -webkit-transform: translateX(54px);
  -ms-transform: translateX(54px);
  transform: translateX(54px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
  $(document).ready(function() {
    $('.switch.switch1').change(function() {
      console.log('text')
    });
  });
</script>



<div style="display: flex; justify-content: space-around; text-align: center; height: 100px;">
  <h3 style="margin-top: 10px">Random text
    <h3>
      <label class="switch switch1">
            <input type="checkbox">
            <span class="slider round"></span>
        </label>
</div>
<div class="section1" style="margin-bottom: 50px; display: none;">
  <h3 style="text-align: center; margin-bottom: 10px;">Random text</h3>
  <div style="display: flex; justify-content: space-around;">
    <textarea style="width: 35%; height: 250px;"></textarea>
  </div>
</div>

Upvotes: 2

Related Questions