Carl Manere
Carl Manere

Reputation: 51

How do i reuse a function several times on one page?

I would like to stop a function so it can run multiple times on one page. It is some radio buttons which i would like to be cleared if a checkbox is clicked. It should work with an infinite number of products. I'm the worst on javascript so i hope i could get an answer?

Sample: See this image:

$('.product .question-input').change(function() {
  if ($(this).is(':checked')) { //radio is now checked
    $('.product .question-checkbox').prop('checked', false);
  }
  return false;
});

$('.product .question-checkbox').change(function() {
  if ($(this).is(':checked')) {
    $('.product .question-input').prop('checked', false);
  }
  return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="product">
  <div class="form-inline justify-content-center">
    <div class="container text-center">
      <div class="product-title">Adobe</div>
    </div>
    <div class="form-group">
      <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">1
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-1" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">2
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-2" value="2"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">3
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-3" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">4
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-4" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-1">5
        <input class="form-check-input question-input" type="radio" name="test-1" id="test-5" value="1"></label></div>
    </div>
    
  </div>
  <div class="form-group form-check text-center the-checkbox">
    <input type="checkbox" name="check-1" class="form-check-input question-checkbox" id="check-1">
    <label class="form-check-label" for="check-1">I don't use this product for work</label>
  </div>
</div>
</div>

<div class="product">
  <div class="form-inline justify-content-center">
    <div class="container text-center">
      <div class="product-title">Mocups</div>
    </div>
    <div class="form-group">
      <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">1
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-1" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">2
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-2" value="2"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">3
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-3" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">4
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-4" value="1"></label></div>
        <div class="form-check form-check-inline">
        <label class="form-check-label" for="test-2">5
        <input class="form-check-input question-input" type="radio" name="test-2" id="test-5" value="1"></label></div>
    </div>
    
  </div>
  <div class="form-group form-check text-center the-checkbox">
    <input type="checkbox" name="check-1" class="form-check-input question-checkbox" id="check-1">
    <label class="form-check-label" for="check-1">I don't use this product for work</label>
  </div>
</div>
</div>

Upvotes: 5

Views: 193

Answers (2)

Rok Sprogar
Rok Sprogar

Reputation: 2401

You can not to this with the same class names for every group, you need to separate the classes in html and create functions for each of them like this:

$('.product1 .question-checkbox1').change(function() {
  if ($(this).is(':checked')) {
    $('.product1 .question-input1').prop('checked', false);
  }
  return false;
});

end then product2, product3...

Upvotes: 1

Effective Robot
Effective Robot

Reputation: 406

By passing a reference to a handler instead of directly passing the handler function:

function handler() {
  if ($(this).is(':checked')) {
    $('.product .question-input').prop('checked', false);
  }
  return false;
}

$('.product .question-input').change(handler);

$('.product .question-checkbox').change(handler);

Upvotes: 3

Related Questions