I have a question concerning the Syntax of outerHTML. It is also possible that I am thinking in the wrong direction.
My html file is as follows:
<!DOCTYPE html>
<html lang="en">
<body>
<label for="language">Choose a language:</label>
<select name="language" id="language" value="val1">
<option value="val1">English</option>
<option value="val2">German</option>
</select>
<p>You selected: <span id="language"></span></p> <!-- shall display either "English" or "German" -->
<p>You selected the following option: <span id="language"></span></p> <!-- shall display either "val1" or "val2" -->
<script src="./script.js"></script>
</body>
</html>
I am referring to a script where for the moment the only content is
var selectedlang = document.getElementById("language").outerHTML;
Within the html file it shall show the value and variable for it. I don't know how to proceed.
You can't have more than one element in a document with the same
idvalue; your current markup usesid="language"on three different elements. You'll need to change two of them at least.I think you're asking how to:
Show the currently-selected option's text and value in the two
spans, andHow to update what you show if the user changes the selection.
If you just wanted the selected value, you could use the
valueproperty of theselectelement. But for both text and value, you'll want theselectedIndexproperty and theoptionscollection:In that example, I've had it accept the
selectandspans as parameters to the function.You'd call that function on page load, and then again whenever the
select'sinputevent fired.Here's an example:
(Note that I changed all three
ids.)