|
371 | 371 | return urlParams.get(name);
|
372 | 372 | }
|
373 | 373 |
|
| 374 | + // Helper function to safely find input by name and value |
| 375 | + function findInputByValue(name, value) { |
| 376 | + const inputs = form.querySelectorAll(`input[name="${name}"]`); |
| 377 | + for (const input of inputs) { |
| 378 | + if (input.value === value) { |
| 379 | + return input; |
| 380 | + } |
| 381 | + } |
| 382 | + return null; |
| 383 | + } |
| 384 | + |
374 | 385 | // Function to update URL with current selections
|
375 | 386 | function updateUrl() {
|
376 | 387 | const dataRegionInputs = form.querySelectorAll('input[name="dataRegion"]:checked');
|
|
383 | 394 | const emailServices = emailServicesInputs.length > 0 ? emailServicesInputs[0].value : '';
|
384 | 395 | const smsServices = smsServicesInputs.length > 0 ? smsServicesInputs[0].value : '';
|
385 | 396 |
|
386 |
| - const urlParams = new URLSearchParams(); |
| 397 | + // Start with existing URL parameters to preserve other query params |
| 398 | + const urlParams = new URLSearchParams(window.location.search); |
387 | 399 |
|
| 400 | + // Update only the parameters this form controls |
388 | 401 | if (dataRegion) {
|
389 | 402 | urlParams.set('data-region', dataRegion.toLowerCase().replace(/\s+/g, '-'));
|
| 403 | + } else { |
| 404 | + urlParams.delete('data-region'); |
390 | 405 | }
|
391 | 406 | if (services.length > 0) {
|
392 | 407 | urlParams.set('services', services.join(','));
|
| 408 | + } else { |
| 409 | + urlParams.delete('services'); |
393 | 410 | }
|
394 | 411 | if (emailServices) {
|
395 | 412 | urlParams.set('email-services', emailServices.toLowerCase().replace(/\s+/g, '-'));
|
| 413 | + } else { |
| 414 | + urlParams.delete('email-services'); |
396 | 415 | }
|
397 | 416 | if (smsServices) {
|
398 | 417 | urlParams.set('sms-services', smsServices.toLowerCase().replace(/\s+/g, '-'));
|
| 418 | + } else { |
| 419 | + urlParams.delete('sms-services'); |
399 | 420 | }
|
400 | 421 |
|
401 | 422 | const newUrl = window.location.pathname + (urlParams.toString() ? '?' + urlParams.toString() : '');
|
|
411 | 432 |
|
412 | 433 | if (dataRegion) {
|
413 | 434 | const dataRegionValue = dataRegion.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
|
414 |
| - const dataRegionInput = form.querySelector(`input[name="dataRegion"][value="${dataRegionValue}"]`); |
| 435 | + const dataRegionInput = findInputByValue('dataRegion', dataRegionValue); |
415 | 436 | if (dataRegionInput) {
|
416 | 437 | dataRegionInput.checked = true;
|
417 | 438 | }
|
|
421 | 442 | const serviceArray = services.toLowerCase().split(',');
|
422 | 443 | serviceArray.forEach(service => {
|
423 | 444 | const serviceValue = service.charAt(0).toUpperCase() + service.slice(1);
|
424 |
| - const serviceInput = form.querySelector(`input[name="services"][value="${serviceValue}"]`); |
| 445 | + const serviceInput = findInputByValue('services', serviceValue); |
425 | 446 | if (serviceInput) {
|
426 | 447 | serviceInput.checked = true;
|
427 | 448 | }
|
|
437 | 458 | } else {
|
438 | 459 | emailServicesValue = emailServices.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
|
439 | 460 | }
|
440 |
| - const emailServicesInput = form.querySelector(`input[name="emailServices"][value="${emailServicesValue}"]`); |
| 461 | + const emailServicesInput = findInputByValue('emailServices', emailServicesValue); |
441 | 462 | if (emailServicesInput) {
|
442 | 463 | emailServicesInput.checked = true;
|
443 | 464 | }
|
|
452 | 473 | } else {
|
453 | 474 | smsServicesValue = smsServices.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
|
454 | 475 | }
|
455 |
| - const smsServicesInput = form.querySelector(`input[name="smsServices"][value="${smsServicesValue}"]`); |
| 476 | + const smsServicesInput = findInputByValue('smsServices', smsServicesValue); |
456 | 477 | if (smsServicesInput) {
|
457 | 478 | smsServicesInput.checked = true;
|
458 | 479 | }
|
|
0 commit comments