JavaScript Javascript item method of Select javascript item method

HTML:

<select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option>Pineapple</option>
    <option>Banana</option>
</select>

Javascript:

var x=document.getElementById("mySelect");
alert(x.options.item(1).text);
alert(x.options[1].text);

Here we are getting the text of option. What is the difference between

x.options.item(1).text

and

x.options[1].text

as both are yielding same result.

Answer:1

SELECT element has a collection of options.

When you do options[i] you're actually accessing collection directly (e.g. array).

When you do options.item(i) you're accessing same thing but through API.

Second way is recommended because if underlying implementation of collection changes you might get exceptions. That's why there is an API.

API spec: https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionsCollection

Answer:2

The options collection returns a collection of all the options in a dropdown list.

x.options.item(index)       //Returns the element from the collection with the specified index

x.options[index]            // will returns element from collection with specified collection .index is an integer specifies the element to retrieve (starts at 0)

So .text will return the same in both the cases

Here is a list of methods of collection Select options Collection

[index]                 An integer that specifies the element to retrieve (starts at 0)
[add(element[,index])]  Adds an option element into the collection at the specified index. If no index is specified, it inserts the option element at the end of the collection
item(index)             Returns the element from the collection with the specified index
namedItem(name)         Returns the element from the collection with the specified name (name or id attribute)
remove(index)           Removes the element with the specified index from the collection
Answer:3

So let's say we have the following XML structure: <property> <label>Label 1</label> <value>value 1</label> </property> <property> <label>...

So let's say we have the following XML structure: <property> <label>Label 1</label> <value>value 1</label> </property> <property> <label>...

  1. can getelementsbytagname return null
  2. what does getelementsbytagname return
  3. what does document getelementsbytagname return

I had a simple script working using Promises in ECMA6 but I'm now converting what I wrote to jQuery to be compatible for all browsers but the issue I'm having is that availableDevices contains an ...

I had a simple script working using Promises in ECMA6 but I'm now converting what I wrote to jQuery to be compatible for all browsers but the issue I'm having is that availableDevices contains an ...

  1. jquery function after page load
  2. jquery function after delay
  3. jquery function after load
  4. jquery function after function complete
  5. jquery function after .each() has completed
  6. jquery function after function
  7. jquery function after animate
  8. jquery function after append
  9. jquery function after fadeout
  10. jquery function after another
  11. jquery function after timeout
  12. jquery function after time
  13. jquery function after ajax load
  14. jquery function after click event
  15. jquery function after page load complete
  16. jquery function after click
  17. jquery function after load html
  18. jquery function after dom load
  19. jquery function after submit
  20. jquery function after addclass

My application uses iframes to display content. I need to detect whether a page is loaded in an iframe. The problem is I need to do it with php, javascript manipulation of the DOM is not enough, I ...

My application uses iframes to display content. I need to detect whether a page is loaded in an iframe. The problem is I need to do it with php, javascript manipulation of the DOM is not enough, I ...

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me. At this point in time I've started writing the initial app in the book in ...

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me. At this point in time I've started writing the initial app in the book in ...