views:

101

answers:

2

This is doing my head in!

I have a select element with several options, however I cannot manipulate or access any of the options, as apparently the 'options' array does not exist.

I have recreated the problem in a simpler html document: (original project is ASP MVC)

<html><head><title>test test test</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" >
function checkMenu() {
if( $("#menu").options ) {
    $("#message").text("Success!");
}
else {
    $("#message").text("Fail :<");
}
}
</script>
</head>
<body>
    <select id="menu" name="menu">
        <option>qwerty</option>
        <option>uiop</option>
        <option>asdf</option>
        <option>ghjkl</option>
    </select>
    <a href="#" onclick="checkMenu()">Clicker!</a>
    <div id="message">message</div>
</body>
</html>

I have not managed to find any info on this, any tips appreciated.

+8  A: 
if($'#menu option').length > 0) {
    $("#message").text("Success!");
}
else {
    $("#message").text("Fail :<");
}
munch
+2  A: 

The $ function in jQuery returns the jQuery object, not the underlying DOM element. If you want to use the underlying DOM element use $('#menu').get(0) or $('#menu')[0]. Even better, use the jQuery methods to access the elements attributes or value.

var selected = $('#menu').value();
var selectedText = $('#menu option:selected').text();
tvanfosson