Here is a snippet of code I've been using to send the value of an <option>
:
function getXhr() {
var xhr = null;
if(window.XMLHttpRequest) // Firefox et autres
xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
xhr = false;
}
return xhr;
}
function go() {
var xhr = getXhr();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
leselect = xhr.responseText;
document.getElementById('modelecontainer').innerHTML = leselect;
}
}
// Here we are working on how to do a post request
xhr.open("POST","contenu_a_charger.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sel = document.getElementById('vehicule');
idmarque = sel.options[sel.selectedIndex].value;
xhr.send("idMarque="+idmarque);
}
Now, I'm looking to modify the sel
variable in my existing code to obtain the ID value of an <li>
.
This is what my <li>
element looks like (generated dynamically through PHP):
<li id="7" onclick="go()">
<span class="badge"><?php echo $FM->etat_user;?></span>
<span class="head"><?php echo $FM->login_user;?></span>
</li>
And here is my old working select element:
<select name="vehicule" id="pays" onchange="go()">
<option value="0">Select your test</option>
<option value="3">test3</option>
<option value="1">test1</option>
<option value="2">test2</option>
</select>
How can I retrieve the ID value of the li
element ("7" in this example)?