Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:19:51+00:00 2026-06-14T16:19:51+00:00

I know how to get value/text of the selected item in a drop down:

  • 0

I know how to get value/text of the selected item in a drop down:

document.getElementById('selNames').options[document.getElementById('selNames').selectedIndex].value

AND

document.getElementById('selNames').options[document.getElementById('selNames').selectedIndex].text

This is really big code. So I have created a function named “$$” which eases this quite:

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

And using it as following to retrieve value and text respectively:

$$('selNames').options[$$('selNames').selectedIndex].value
$$('selNames').options[$$('selNames').selectedIndex].text

But I further want to minimize this code as following:

$$('selNames').val
$$('selNames').text

I know jQuery too but I don’t want to use it because sometimes I don’t require that much functionality that jQuery is providing and to use lesser file size for faster loading of page resources.

So, how do I make the “$$” object that can act as I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:19:53+00:00Added an answer on June 14, 2026 at 4:19 pm
    function $$(id) { 
        var el = document.getElementById(id);
        return {
            element: el,
            get val() {
                return el.options[el.selectedIndex].value;
            },
            get text() {
            return el.options[el.selectedIndex].text;
            }
        };
    }
    

    If you’re not averse to some prototype fiddling, you can use this:

    HTMLSelectElement.prototype.__defineGetter__("val", function() { return this.options[this.selectedIndex].value; });
    HTMLSelectElement.prototype.__defineGetter__("text", function() { return this.options[this.selectedIndex].text; });
    

    Here is a demonstration: http://jsfiddle.net/Ynb8j/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how can I get the value of the selected option
i know there are a lot questions like this but i really cant get
I want to know how to get the value of the selected rawquery Cursor
I get color value with jQuery with .css('color') , and then I know color
Does anyone know how I can get the value of an input box, without
Does someone know why i never get the first value of my array? it
i want know if is possible, to get a specific element value of a
I need to get the value of the selected option (when changed) in a
I know should be really simple, but I just don't get it. I'm using
Can someone please let me know how to get values from several input fields?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.