0

Hello I am trying to write the following code with and elements to display some options on the form using visual studio code editor. My code editor is not displaying the "options" property after dot operator in an array syntax, it is showing option. Here is the original code.

<html>
<head>
  <title>DOM Tests</title>
  <script>
    function setBodyAttr(attar, value) {
      if (document.body) document.body[attar] = value;
      else throw new Error("no support");
    }
  </script>
</head>
<body>
  <div style="margin: .5in; height: 400px;">
    <form>
      <p><b><code>text</code></b></p>
      <select onChange="setBodyAttr('text',
        this.options[this.selectedIndex].value);">
        <option value="black">black</option>
        <option value="red">red</option>
      </select>
      
      <small>
        <a href="http://some.website.tld/page.html" id="sample">
          (sample link)
        </a>
      </small><br />
      <input type="button" value="version" onclick="ver()" />
    </form>
  </div>
</body>
</html>

//following screenshot is on the vscode. Screenshot with option property

Andor Polgar
  • 483
  • 3
  • 13
Iram
  • 29
  • 8
  • 1
    Chances are, intellisense is unable to infer that `this` refers to the select element. If you hover over it, you will realize it is inferred as `globalThis`. Speaking of which, if you are to abstract all that logic away from inline JS, the issue will resolve itself, since `this` will resolve to `any` instead. – Terry Jun 29 '21 at 07:19
  • Inline event handlers like `onchange` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Jun 29 '21 at 15:36

0 Answers0