By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,859 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,859 IT Pros & Developers. It's quick & easy.

Trying to capture the value of Select options List

P: 1
I am one of those old school dinosaur types that has been around since the great depression and to be perfectly honest with you I am very comfortable with JavaScript and do not revert to plug-ins such as jQuery when I can get the same job done with pure clean JavaScript, but when I am trying to capture values from select options lists I used to use
var varName= document.formName.selectName.options[selectName.selectedIndex].value
I LIKE THIS OLD FASHION WAY BECAUSE IT IS CLEAN AND EASY TO UNDERSTAND and even though I do use jQuery when I have no better choices the truth I find is that the additional lines of code that jQuery saves me isn't enough to be significant, and I am not a lazy coder...
But I am finding that doesn't work any more and now I am finding it more and more difficult to do the kind of coding I am used to, so any help would be very much appreciated???
I have to be leaving something out, I am sure the problem is with syntax but I haven't found it yet, and the same code seems to work on older web pages just fine.
By the way I am using windows 10/64
Nov 4 '17 #1
Share this Question
Share on Google+
2 Replies

P: 5
Simply use
Expand|Select|Wrap|Line Numbers
  1. alert($("#selectListId").val())
. It will show the selected value in the alert box.
Feb 1 '18 #2

Expert Mod 5K+
P: 5,389
well - the OP didn't want to use jQuery for such a simple operation - but even noone answered the OPs question until now. So a simple example to achieve it with vanilla javascript can be found here:

so if the only thing that would have to be done would be to retrieve the value of a select-element then including jQuery for this line that basically does the 'magic':

Expand|Select|Wrap|Line Numbers
  1. var x = document.getElementById("mySelect").value;

would create the overhead of loading that lib for that only. Besides that - the (jQuery-) $-function obviously wraps exactly the plain DOM-methods - but this way it can only be slower - since basically you call a facade that dispatches to the correct DOM-method in the end. This is of course a theoretical issue - but when it comes to a point where you would use such facade-methods in a loop it can be significantly slower.
Feb 1 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.