Limit Services Addition
November 19, 2024 · AI Conversations
$("#moreservicediv").append(`
+
–
`);
});
limit it to 3
let serviceCount = 0; // Initialize a counter
$('.geodir-add-listing').on('click', '.addmoreservice', function() {
if (serviceCount < 3) { // Check if the limit is reached
$("#moreservicediv").append(`
<div class="service-item">
<input type="text" name="services_offered_service_name[]" id="services_offered_service_name" class="form-control">
<a href="javascript:void(0)" class="addmoreservice">+</a>
<a href="javascript:void(0)" class="removemoreservice">-</a>
</div>
`);
serviceCount++; // Increment the counter
} else {
alert('You can only add up to 3 services.');
}
});
// Handle removing services and updating the counter
$('.geodir-add-listing').on('click', '.removemoreservice', function() {
$(this).closest('.service-item').remove(); // Remove the specific service
serviceCount--; // Decrement the counter
});
### Explanation:
1. **Counter Initialization**: `serviceCount` is initialized to 0 to keep track of the number of added services.
2. **Limit Check**: Before appending a new service, check if `serviceCount` is less than 3. If not, display an alert.
3. **Counter Increment**: When a service is added, increment `serviceCount`.
4. **Service Removal**: Decrease `serviceCount` when a service is removed by clicking the `removemoreservice` button.
let serviceCount = 1; // Initialize the counter to 1 because one field is predefined
$('.geodir-add-listing').on('click', '.addmoreservice', function() {
if (serviceCount < 3) { // Allow adding more fields only if the count is below 3
$("#moreservicediv").append(`
<div class="service-item">
<input type="text" name="services_offered_service_name[]" id="services_offered_service_name" class="form-control">
<a href="javascript:void(0)" class="addmoreservice">+</a>
<a href="javascript:void(0)" class="removemoreservice">-</a>
</div>
`);
serviceCount++; // Increment the counter
} else {
alert('You can only add up to 3 services.');
}
});
// Handle removing services and updating the counter
$('.geodir-add-listing').on('click', '.removemoreservice', function() {
$(this).closest('.service-item').remove(); // Remove the specific service
serviceCount--; // Decrement the counter
});
### Key Adjustments:
1. **`serviceCount` Initialized to `1`**: Start the counter at 1 to account for the pre-defined service field.
2. **Logic for Adding Services**: Ensure the total fields (predefined + dynamically added) do not exceed 3.
3. **Removal Logic**: Decrease the counter when a service field is removed.
This will correctly handle the predefined field and ensure the total number of fields doesn't exceed 3.
let serviceCount = 1; // Initialize the counter to 1 because one field is predefined
$('.geodir-add-listing').on('click', '.addmoreservice', function() {
if (serviceCount < 3) { // Allow adding more fields only if the count is below 3
let newServiceHtml = `
<div class="service-item">
<input type="text" name="services_offered_service_name[]" id="services_offered_service_name" class="form-control">
`;
// Add the "+" button only if it's not the 3rd item
if (serviceCount < 2) {
newServiceHtml += `<a href="javascript:void(0)" class="addmoreservice">+</a>`;
}
// Add the "-" button for all items
newServiceHtml += `<a href="javascript:void(0)" class="removemoreservice">-</a></div>`;
$("#moreservicediv").append(newServiceHtml);
serviceCount++; // Increment the counter
} else {
alert('You can only add up to 3 services.');
}
});
// Handle removing services and updating the counter
$('.geodir-add-listing').on('click', '.removemoreservice', function() {
$(this).closest('.service-item').remove(); // Remove the specific service
serviceCount--; // Decrement the counter
// Ensure the "+" button is shown again if items are less than 3
if (serviceCount < 3) {
$('.addmoreservice').last().show();
}
});
### Adjustments:
1. **Conditional `+` Button**: The `+` button is included only if the total service count is less than 3. For the third service, it's omitted.
2. **Ensure `+` Reappears**: When a service is removed, the `+` button for the last service is ensured to reappear if the count falls below 3.
3. **Dynamic Logic**: The HTML for each new service dynamically adapts based on the current count.
This ensures the `+` button is hidden for the third item and reappears when necessary.
$('.geodir-add-listing').on('click','.addmoreservice',function() {
if (serviceCount < 3) {
$("#moreservicediv").append(`
+
–
`);
serviceCount++;
}
});
if we remove one then again need to add upon + click
let serviceCount = 1; // Start with 1 because one service field is predefined
$('.geodir-add-listing').on('click', '.addmoreservice', function() {
if (serviceCount < 3) { // Allow adding only up to 3 fields
$("#moreservicediv").append(`
<div class="service-item">
<input type="text" name="services_offered_service_name[]" id="services_offered_service_name" class="form-control">
<a href="javascript:void(0)" class="addmoreservice">+</a>
<a href="javascript:void(0)" class="removemoreservice">-</a>
</div>
`);
serviceCount++; // Increment the count
updateAddMoreButton(); // Ensure the "+" button visibility logic is correct
}
});
// Remove a service field and adjust the count
$('.geodir-add-listing').on('click', '.removemoreservice', function() {
$(this).closest('.service-item').remove(); // Remove the service field
serviceCount--; // Decrement the count
updateAddMoreButton(); // Ensure the "+" button visibility logic is correct
});
// Function to manage "+" button visibility
function updateAddMoreButton() {
// If the maximum number of fields is reached, hide all "+" buttons
if (serviceCount >= 3) {
$('.addmoreservice').hide();
} else {
// Show the "+" button for the last service item only
$('.addmoreservice').show();
$('.addmoreservice').last().show();
}
}
// Initial call to set the "+" button visibility correctly
updateAddMoreButton();
### Explanation:
1. **Dynamic Count Adjustment**: The `serviceCount` variable increases when a field is added and decreases when one is removed.
2. **`updateAddMoreButton()` Function**:
– Hides all `+` buttons if the count reaches 3.
– Ensures the `+` button is visible only for the last service item when the count is less than 3.
3. **Remove Logic**: When a field is removed, the `serviceCount` is updated, and the `+` button for the last item is correctly displayed.
This approach ensures the system dynamically adjusts the visibility and functionality of the `+` button based on the current number of service fields.