3

How do I get the actual value (or text) of the item selected in an HTML select box? Here are the main methods I've tried...


document.getElementById('PlaceNames').value

$("#PlaceNames option:selected").val()

$("#PlaceNames option:selected").text()

And I've tried various variations on these. All I ultimately want to do is get that data and send it to a web service via AJAX, I just need the string representation of what the user selected. This seems like it should be really easy and I've even found a question similar to this here, but I'm still having issues getting it worked out. Google doesn't seem to be helping either. I feel like it must be due to some fundamental misunderstanding of Javascript and jQuery.

EDIT: I should mention that I'm using IE7

3 Answers 3

5

$('#placeNames').val() returns the value of the selected option in the latest version of jQuery.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I'm an idiot. I was doing it this way initially and was getting null returned. Turns out I was using the Id for an empty list box on the page just below the one I should have been using. It's been a long day.
1

Non-jQuery variant:

var select = document.getElementById('PlaceNames');
var value = select.options[select.selectedIndex].value;

Older jQuery variant:

var select = $('#PlaceNames:first')[0];
var value = select.options[select.selectedIndex].value;

Current jQuery variant:

var value = $('#PlaceNames:first').val();

I'm probably just being overly safe using :first.

Comments

0

var plid = $('#PlaceNames').val();

val placename = $('#PlaceNames option[value=\''+plid.toString()+'\']').html();

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.