Hello. I noob programing.

I need extracting "ALGARROBO" and put in variable in JS.

<select>
<option value="0560200101">ALGARROBO</option>
<option value="0760200601">CALIFORNIA</option>
""       ""         ""   +70 options
</select>

Someone would be so kind to give me the js code?
It is not possible to change the "value", since that information goes to api (and created to handle those numbers)
I know how to extract the "value" and use it, but I want to store "Algarrobo" in a variable in JS, how is it done?

Best regards and thanks.

Recommended Answers

Example follows of how to get the ALGARRABO but in this case fruit.

<!DOCTYPE html>
<html>
<body>

<form>
  Select your favorite fruit:
  <select id="mySelect">
    <option value="apple">Apple</option>
    <option value="orange">Orange</option>
    <option value="pineapple">Pineapple</option>
    <option value="banana">Banana</option>
  </select>
</form>

<p>Click the button to return the value of the selected fruit.</p>

<button type="button" …
Jump to Post

All 3 Replies

Example follows of how to get the ALGARRABO but in this case fruit.

<!DOCTYPE html>
<html>
<body>

<form>
  Select your favorite fruit:
  <select id="mySelect">
    <option value="apple">Apple</option>
    <option value="orange">Orange</option>
    <option value="pineapple">Pineapple</option>
    <option value="banana">Banana</option>
  </select>
</form>

<p>Click the button to return the value of the selected fruit.</p>

<button type="button" onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].text;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

What you are asking for appears to be summed up in this line:

var x = document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].text;

Alter the code to match your object names.

Why did you change line 23?

Mine: var x = document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].text;

Yours: var x = document.getElementById("mySelect").parentElement.parentElement.nodeName;

NOTE! If you change the object type, I consider this a NEW QUESTION!!!

commented: Solved, I use console.log out script ahhaha. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.