Example Blog Title

This script listens for changes to the <select> element with the ID p-cill. When the selected value changes, it checks if the value is “Yes” and adds the active class to the .cill element. If the value is “No” or any other

// For Cill $(‘#p-cill’).change(function() { if ($(this).val() === ‘Yes’) { $(‘.cill’).addClass(‘active’); } else { $(‘.cill’).removeClass(‘active’); } }); I want for each .cill class it should do the same thing on one input changeChatGPT

If you want the same functionality to apply to multiple .cill elements, each triggered by their respective input changes, you can modify the script to target the specific .cill element associated with each input. Here’s how you can adapt the code:

HTML Structure (Assuming multiple inputs and .cill containers):

htmlCopy code<div class="form-floating">
    <select class="form-select shadow-none mb-3 p-cill" aria-label="Floating label select example">
        <option selected="" disabled>Select</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
    </select>
    <label for="p-cill">Cill 1</label>
</div>
<div class="cill">
    <!-- Content for cill 1 -->
</div>

<div class="form-floating">
    <select class="form-select shadow-none mb-3 p-cill" aria-label="Floating label select example">
        <option selected="" disabled>Select</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
    </select>
    <label for="p-cill">Cill 2</label>
</div>
<div class="cill">
    <!-- Content for cill 2 -->
</div>

jQuery:

javascriptCopy code$(document).ready(function() {
    $('.p-cill').change(function() {
        var index = $('.p-cill').index(this); // Get the index of the changed select

        if ($(this).val() === 'Yes') {
            $('.cill').eq(index).addClass('active'); // Add active class to corresponding .cill
        } else {
            $('.cill').eq(index).removeClass('active'); // Remove active class from corresponding .cill
        }
    });
});

CSS (Optional):

You can style the .active class as needed to show or hide the .cill elements:

cssCopy code.cill {
    display: none; /* Hidden by default */
}

.cill.active {
    display: block; /* Show when active */
}

Explanation:

  • HTML Structure: Each .form-floating contains a <select> element with the class .p-cill and a corresponding .cill container.
  • jQuery: The script targets all .p-cill elements and listens for their change events. When an input changes, it determines its index within all .p-cill elements using index(). It then adds or removes the active class from the .cill element at the same index using eq(index).

This approach ensures that each .cill element toggles its active class independently based on the selection of its associated <select> element. Adjust

// For Cill $(‘#p-cill’).change(function() { if ($(this).val() === ‘Yes’) { $(‘.cill’).addClass(‘active’); } else { $(‘.cill’).removeClass(‘active’); } }); instead of on change wrap this in a function and check if it is yes… so add the class else remove the class… and run the function after each 1 sChatGPT

If you want to encapsulate the functionality of toggling the .active class based on the value of #p-cill (or any similar selector) into a function and run it every second, you can do the following:

jQuery Function and Timer Setup:

javascriptCopy codefunction toggleCillClass() {
    if ($('#p-cill').val() === 'Yes') {
        $('.cill').addClass('active');
    } else {
        $('.cill').removeClass('active');
    }
}

// Run the function immediately
toggleCillClass();

// Run the function every 1 second
setInterval(function() {
    toggleCillClass();
}, 1000);

Explanation:

  1. Function Definition: toggleCillClass() encapsulates the logic to check the value of #p-cill and add or remove the active class from .cill accordingly.
  2. Initial Execution: toggleCillClass() is called once immediately when the script runs to apply the initial state.
  3. Interval Timer: setInterval() is used to repeatedly call toggleCillClass() every 1000 milliseconds (1 second), ensuring that the .active class is updated based on the current value of #p-cill.

Notes:

  • Ensure that #p-cill exists in your HTML and is correctly updated to reflect changes in its selected option.
  • Adjust the interval (1000 milliseconds in this case) to your desired timing for updating the class.

This setup will continuously check the value of #p-cill and update the .cill elements accordingly every second.

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping