seeTheObvious
seeTheObvious

Reputation: 663

Javascript not acquiring dropdown value

I have a list of 2 values in dropdown list as...

      <select id="prop">
          <option value="Caa:123">Ca</option>
          <option value="Cb:745">Cb</option>
      </select>

...and in javascript i used...

       var p = document.getElementById('prop');
       var q = p.options[p.selectedIndex].value;
       alert(q);

...but I am not getting no alert and an error "Index or size is negative or greater than the allowed amount" code: "1 " Kindly help I trapped in this problem

Upvotes: 1

Views: 100

Answers (2)

Kalle H. V&#228;ravas
Kalle H. V&#228;ravas

Reputation: 3615

This wasn't in your questions specifications, but I would use jQuery. It definitely is much more easier to handle and looks neater:

<select id="prop">
    <option value="Caa:123">Ca</option>
    <option value="Cb:745">Cb</option>
</select>
<script>
    alert($('#prop').val());

    $('#prop').change(function () {
        alert($('#prop').val());
    });
</script>

[ View output ]

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

Try like this:

var p = document.getElementById('prop');
var q = p.value;
alert(q);

Upvotes: 1

Related Questions