9

My HTML code is as:

 <div id="detail">
    <div class="d_left">
    Page <strong>1</strong> of 107
    </div>
    <div class="d_center">
    <table>
    <tr>
    <td><a href="#">Previous</a> | <a href="#">Next</a>
    <img src="/webProject/store/images/arrow.png" align="absmiddle" alt="">
    </td></tr></table>
    </div>
    <div class="d_right">
    Sort by:
     <select name="featured" size="1" id="item1">
          <option>Featured Items1</option>
          <option>Featured Items2</option>
          <option>Featured Items3</option>
          <option>Featured Items4</option>
        </select>
    </div>
</div>

Now, I want to read selected value from <select name="featured" size="1" id="item1">. How can I do this using JavaScript?

1
  • When you want to read it? In response to what event? Commented Jul 13, 2011 at 8:09

4 Answers 4

15
document.getElementById("item1").value;
Sign up to request clarification or add additional context in comments.

3 Comments

James Allardice: You mean there is no need to do like detai1.item1
@Makram this wont work as the current html doesnot contains value in options.
@sushil bharwani Actually it will work, because the first option is always selected - jsfiddle.net/VUNb3
5

More Elegant variant of sushil bharwani solution

function $(id){return document.getElementById(id);}

var select = $("item1");
select.onchange = function() {
    var selIndex = select.selectedIndex;
    var selValue = select.options(selIndex).innerHTML;
}

As Shadow Wizard do not like this solution I hope he will like that:

var select = document.getElementById("item1");
select.onchange = function() {
    var selIndex = select.selectedIndex;
    var selValue = select.options(selIndex).innerHTML;
}

The main idea in these two example is to reduce the usage of getElementById. No point to execute it more than once - thus minimizing the access to the DOM.

For those who feel brave enough :) there is this new thing querySelector()mdn, msdn IE dev center, msdn, w3 spec:

var select = document.querySelector("#item1");

4 Comments

Where did you see jQuery? Why you assume it can be used?
@Shadow Wizard it is pure JavaScript - no jQuery involved here. It is not a selector it is a short for getElementById. The function name can start with a-z A-Z $ _
Sorry guys I used to work with IE7 for far too long saw now that options can be also accessed as a function and the $ stuff also confused me. Vote undone, will put more time to reading and testing in the future.
select.options(selIndex) this is not work, select.options[selIndex] this is work
3

document.getElementById("item1").onchange = function(){
var selIndex = document.getElementById("item1").selectedIndex;
var selValue = document.getElementById("item1").options[selIndex].innerHTML;
}

2 Comments

Two errors in one line: options is array and option element has no innerHTML just value or text.
OK, now that you fixed the array part (using square brackets) it's working in all major browsers - innerHTML didn't work for me long time ago guess it was "fixed" in IE7 and above.
2
function delivery(x){
      var country = x.value;
      document.getElementById('lala').innerHTML = country;
}
<div id="lala"></div>
<form action="" method="post">
<select name="country" id="country" onChange="delivery(this)">
    <option value='lala'>Select Country</option>
    <option value='United_Kingdom'>United Kingdom</option>
    <option value='Russia'>Russia</option>
    <option value='Ukraine'>Ukraine</option>
    <option value='France'>France</option>
    <option value='Spain'>Spain</option>
    <option value='Sweden'>Sweden</option>
</select>
</form>

Here is how i solved my problem for selecting eu countries (i have removed most of them to make my codes shorter).. as im new to javascript.. i still need to learn javascript libraries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.