Html option caption

Hi, Experts, How to change the option caption using javascript? Base on my code, I want to change the option caption from "Yes" to "No". Thanks!

<!DOCTYPE html>
<html>
<body>
<button onclick="return ChangeCaption()">Change</button>
<select name="level" id="iAD8" style="color: black; width:50px;">
    <option value="yes" selected>Yes</option>
    <option value="no">No</option>
</select>

<script>
function ChangeCaption()
{
  document.getElementById("iAD8").value = "No"; 
}
</script>

</body>
</html>

Open in new window

Whing Dela CruzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zakaria AcharkiAnalyst DeveloperCommented:
You was so close, you need just to use the value instead of the text so replace  `No` by `no` :

document.getElementById("iAD8").value = "no";

Open in new window


Working fiddle

As a best practice I would suggest to you the use of `.addEventListener()` to attach events instead of inline event `onclick` :

<html>

  <body>
    <button id="my-button">Change</button>
    
    <select name="level" id="iAD8" style="color: black; width:50px;">
        <option value="yes" selected>Yes</option>
        <option value="no">No</option>
    </select>

    <script>
      document.getElementById("my-button").addEventListener('click', function() {
        document.getElementById("iAD8").value = "no";
      })
    </script>
  </body>

</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Whing Dela CruzAuthor Commented:
Perfectly working, thanks a lot sir!
Zakaria AcharkiAnalyst DeveloperCommented:
You're welcome, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.