Trying to capture the value of Select options List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vegasvato
    New Member
    • Nov 2017
    • 1

    Trying to capture the value of Select options List

    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.formNa me.selectName.o ptions[selectName.sele ctedIndex].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
  • RockybBalboa
    New Member
    • Feb 2018
    • 5

    #2
    Simply use
    Code:
    alert($("#selectListId").val())
    . It will show the selected value in the alert box.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      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:

      The W3Schools online code editor allows you to edit code and view the result in your browser


      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':

      Code:
      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.
      Last edited by gits; Feb 1 '18, 11:53 AM.

      Comment

      Working...