1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| const TexteTabcontent = document.getElementById("commentaire"),
ElementSel = document.querySelector(".tabulation"),
InputPaysGenres = document.querySelectorAll(".checkboxes"),
multiselectP = document.querySelectorAll(".reponse"),
recherche = document.getElementById("codepostalcommune"),
listecommunes = document.getElementById("listecommunes")
function raz() {
TexteTabcontent.textContent = ''
recherche.innerText = ''
for (let i = 0; i < multiselectP.length; i++) {
multiselectP[i].innerHTML = ''
}
while (listecommunes.options.length > 0) {
listecommunes.remove(0)
listecommunes.disabled = true
}
}
for (let i = 0; i < InputPaysGenres.length; i++) {
selpaysgenres[i].addEventListener("mouseover", function (event) {
let el = event.target
TexteTabcontent.innerHTML = el.textContent
})
selpaysgenres[i].addEventListener("mouseleave", function () {
TexteTabcontent.innerHTML = ''
})
}
//click bouton
ElementSel.addEventListener("click", function (event) {
let el = event.target,
tabcontent = document.getElementsByClassName("tabcontent"),
IdOnglet = document.getElementById("tab_" + el.getAttribute("id")),
alltablinks = document.querySelectorAll(".tablinks")
do {
if (el.id === "commune") {
alltablinks.forEach(function (tablinks, index) {
if (tablinks.id !== el.id) {
tablinks.classList.remove('active')
tabcontent[index].style.display = 'none'
}
})
if (el.className === "tablinks active") {
el.classList.remove("active")
IdOnglet.style.display = 'none'
TexteTabcontent.innerHTML = ''
} else {
el.classList.add("active");
IdOnglet.style.display = 'block'
}
TexteTabcontent.innerHTML = el.textContent;
}
if (el.id === "codeOrcommune") {
TexteTabcontent.classList.remove("msgerreur");
TexteTabcontent.textContent = ''
raz()
let inputselect = document.getElementById(el.previousElementSibling.id),
valeurinput,
pattern,
result,
datasearch
valeurinput = inputselect.value
if (valeurinput == "") return
pattern = /[a-z A-Z]+\D/g // Nom de commune
result = valeurinput.match(pattern)
if (result !== null) {
datasearch = "https://geo.api.gouv.fr/communes?nom="
} else {
pattern =
/(^[0-9]\d{0}([0-5]\d{0}|[7-9]\d{0})([0-9]\d{0}[0-9]\d{0}[0-9]\d{0}))/g
result = valeurinput.match(pattern)
if (result !== null && result >= 1000 && result <= 97680) {
datasearch = "https://geo.api.gouv.fr/communes?codePostal="
} else {
TexteTabcontent.className = "msgerreur"
TexteTabcontent.innerHTML = "Code Postal inconnu !"
return
}
}
afficheInfos(TexteTabcontent, datasearch, inputselect)
}
if (el.id === "raz") {
multiselectP[0].value = ''
raz()
}
} while ((el = el.parentNode) !== this)
})
function afficheInfos(TextTab, dsearch, dselectinput) {
let apiurl
TextTab.innerHTML = "Résultat pour " + dselectinput.value
apiurl = dsearch + dselectinput.value + "&fields=nom,code,codesPostaux,siren,codeEpci,codeDepartement,region,codeRegion,population,zone&format=json&geometry=centre";
fetch(apiurl)
.then((response) => response.json())
.then((data) => {
Vvalue = data
if (data.length) {
data.forEach((item, index) => {
let opt = document.createElement("option")
opt.value = index
opt.innerHTML = item.nom
listecommunes.appendChild(opt)
})
} else {
TextTab.className = "msgerreur"
TextTab.innerHTML = "Pas de Résultat pour " + dselectinput.value
}
if (listecommunes.options.length > 0) {
listecommunes.disabled = false
optionSelect()
}
})
.catch((error) => {
console.log("Erreur!")
TextTab.className = "msgerreur"
TextTab.innerHTML = "Error! No data"
})
}
function optionSelect() {
var x = listecommunes.value
document.getElementById("nom").innerHTML = Vvalue[x]["nom"]
document.getElementById("siren").innerHTML = Vvalue[x]["siren"]
document.getElementById("codeepci").innerHTML = Vvalue[x]["codeEpci"]
document.getElementById("code").innerHTML = Vvalue[x]["code"]
document.getElementById("coderegion").innerHTML = Vvalue[x]["coderegion"]
document.getElementById("codep").innerHTML = Vvalue[x]["codesPostaux"]["0"]
document.getElementById("zone").innerHTML = Vvalue[x]["zone"]
document.getElementById("depar").innerHTML = Vvalue[x]["codeDepartement"]
document.getElementById("region").innerHTML = Vvalue[x]["region"]["nom"]
document.getElementById("popul").innerHTML = Vvalue[x]["population"]
}
raz() |
Partager