1. No Profile Picture
    Give peace a chance.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Buenos Aires, Argentina
    Rep Power

    Unhappy correct javascript acording to DOM

    I'm converting all my javascript scripts so they're DOM compilant, I was almost successful except there's a line I don't know how to convert.
    There's a select with id="country" with some options (I think you can guess what options are there), so I need to get the value of the selected option, first, I get the object by:
    var countryObj = document.getElementById("country");
    and then I use this line to get the value of the selected option (maybe there's even a better way, I would use it if there's as long as it's dom compliant):
    var countryValue = countryObj.options[countryObj.options.selectedIndex].value;
    Thank you
  2. #2
  3. No Profile Picture
    CF sMod
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Maine, USA (a.k.a. Boonies)
    Rep Power

    The recently released W3C DOM2 HTML Recommendation states that just:

    var selectValue = document.getElementById('selectID').value;

    suffices. I'm pretty sure that works with Gecko browsers (NS6, NS7, Mozilla, Galeon, K-meleon), and it may be a shortcut that IE has allowed for some time. Try it out and see, but the code you posted is completely DOM-complient:

    HTMLSelectElement.options returns an HTMLOptionsCollection, but it doesn't specify for array indexes (just like it doesn't for any NodeList), but browsers allow you.

    and selectedIndex is also specified, so your code is fine.
    Jason Contact Me
    Super moderator @ CodingForums

IMN logo majestic logo threadwatch logo seochat tools logo