Commercial Villas for Rent in Umm Salal Mohammad 1 results

// Parse and display existing selected cities if any if (keywordInput.value) { let cities = keywordInput.value.split(',').map(city => city.trim()); cities.forEach(city => { let [SearchName, SearchId, SearchType] = city.split(' ('); console.log(SearchName); SearchType = SearchType ? SearchType.replace(')', '') : ''; SearchId = SearchId ? SearchId.replace(')-', '') : ''; //alert(SearchId + ''+ SearchType); if (SearchName) { addLocationTag(SearchName, SearchId, SearchType); } }); keywordInput.value = ''; // Clear the input field after processing } // Add a city tag to the display function addLocationTag(locName, locId, locType = '') { if (locType === 'city') { let city_id_val = $("#city_id").val(); $("#city_id").val(city_id_val + ',' + locId); } else if (locType === 'subcity') { let sub_city_id_val = $("#sub_city_id").val(); $("#sub_city_id").val(sub_city_id_val + ',' + locId); } else if (locType === 'town') { let town_id_val = $("#town_id").val(); $("#town_id").val(town_id_val + ',' + locId); } else if (locType === 'area') { let area_id_val = $("#area_id").val(); $("#area_id").val(area_id_val + ',' + locId); } let existingIds = selectedItems.map(loc => loc.id); if (existingIds.includes(locId)) { return; // Avoid adding duplicate tags } selectedItems.push({ name: locName, id: locId, type: locType }); // Update tag display instead of appending to input let selectedDiv = document.createElement('div'); selectedDiv.className = 'selected-location'; selectedDiv.dataset.locationId = locId; selectedDiv.dataset.locationType = locType; selectedDiv.innerText = locName; let removeBtn = document.createElement('span'); removeBtn.className = 'remove-btn'; removeBtn.innerHTML = '×'; removeBtn.onclick = function() { let locId = this.parentElement.dataset.locationId; removeLocationTag(locId); this.parentElement.remove(); }; selectedDiv.appendChild(removeBtn); document.getElementById('selected-locations').appendChild(selectedDiv); console.log('Update Keyword Area - 1'); updateKeywordField(); // Update the hidden input fields keywordInput.value = ''; // Clear the input field after selection } // Remove a city tag function removeLocationTag(locId) { selectedItems = selectedItems.filter(item => item.id !== locId); console.log('Update Keyword Area - 2'); updateKeywordField(); // Update the hidden input fields after removal //document.getElementById('myForm').submit(); } // Update hidden input fields with selected items function updateKeywordField() { console.log('Update Keyword Field'); let cityIds = selectedItems.filter(loc => loc.type === 'city').map(loc => loc.id); let subcityIds = selectedItems.filter(loc => loc.type === 'subcity').map(loc => loc.id); let townIds = selectedItems.filter(loc => loc.type === 'town').map(loc => loc.id); let areaIds = selectedItems.filter(loc => loc.type === 'area').map(loc => loc.id); $("#extra_keywords_12").html(` `); } // Handle form submission document.querySelector('form').addEventListener('submit', function(event) { updateKeywordField(); // Ensure hidden fields are up to date }); // Handle autocomplete selection function autocomplete(inp, url) { var currentFocus; inp.addEventListener("input", function(e) { var a, b, i, val = this.value; closeAllLists(); if (!val) { return false; } currentFocus = -1; // Create a DIV element to contain the autocomplete items a = document.createElement("DIV"); a.setAttribute("id", this.id + "autocomplete-list"); a.setAttribute("class", "autocomplete-items"); this.parentNode.appendChild(a); // Fetch data from the server via AJAX $.ajax({ url: url, type: "GET", data: { 'country': val, 'purpose': $("#globalPropertyPurposeValue").val(), 'type': $("#globalPropertyTypeValue").val() }, success: function(data) { if (data) { var items = []; // Populate autocomplete list with fetched items data.cities.forEach(function(item) { items.push({ id: item.id, name: item.name, type: 'city' }); }); data.subcities.forEach(function(item) { items.push({ id: item.id, name: item.name, type: 'subcity' }); }); data.towns.forEach(function(item) { items.push({ id: item.id, name: item.name, type: 'town' }); }); data.areas.forEach(function(item) { items.push({ id: item.id, name: item.name, type: 'area' }); }); // Populate autocomplete list with fetched items for (i = 0; i < items.length; i++) { if (!selectedItems.some(el => el.id === items[i].id)) { b = document.createElement("DIV"); b.innerHTML = "" + items[i].name.substr(0, val.length) + ""; b.innerHTML += items[i].name.substr(val.length); b.innerHTML += ""; b.addEventListener("click", function(e) { addLocationTag( this.getElementsByTagName("input")[0].value, this.getElementsByTagName("input")[0].getAttribute('data-id'), this.getElementsByTagName("input")[0].getAttribute('data-type') ); closeAllLists(); }); a.appendChild(b); } } } } }); }); inp.addEventListener("keydown", function(e) { var x = document.getElementById(this.id + "autocomplete-list"); if (x) x = x.getElementsByTagName("div"); if (e.keyCode == 40) { currentFocus++; addActive(x); } else if (e.keyCode == 38) { currentFocus--; addActive(x); } else if (e.keyCode == 13) { e.preventDefault(); if (currentFocus > -1) { if (x) x[currentFocus].click(); } } }); function addActive(x) { if (!x) return false; removeActive(x); if (currentFocus >= x.length) currentFocus = 0; if (currentFocus < 0) currentFocus = (x.length - 1); x[currentFocus].classList.add("autocomplete-active"); } function removeActive(x) { for (var i = 0; i < x.length; i++) { x[i].classList.remove("autocomplete-active"); } } function closeAllLists(elmnt) { var x = document.getElementsByClassName("autocomplete-items"); for (var i = 0; i < x.length; i++) { if (elmnt != x[i] && elmnt != inp) { x[i].parentNode.removeChild(x[i]); } } } document.addEventListener("click", function(e) { closeAllLists(e.target); }); } // Initialize autocomplete with AJAX URL autocomplete(document.getElementById("country_data"), "https://sg.saakin.qa/search-desktop"); });