Results 1 to 10 of 10

Thread: Best way to act on select option

  1. #1
    Join Date
    Oct 2012

    Best way to act on select option

    Hi guys.

    I'm very new to Javascript although I have done a fair amount of PHP. The two languages seem to be sufficiently different or I'm doing something wrong to ask this question.

    What is the best way to get the value of an option, then act on it according to the value in the option.

    For example if I have

    • Apple
    • Banana
    • Orange

    How do I get the script to do something with oranges?

    Do I use Switch or a series of If/Elseif statements?

    Also should I use the value tag for options as having spaces would cause issues wouldn't they?

    <option value="fruitApple">Apples are nice</option>
    <option value="fruitBanana">Bananas are nice</option>
    <option value="fruitOrange">Oranges are nice</option> 
    If this is the case then how do you select the value?

    Thanks for your help folks.

  2. #2
    Join Date
    Nov 2010
    by putting an onchange event onto the select tag which calls a function. Old school method that is still fine for beginner's purposes:
    <select onchange="showVal(this.value)">
    function showVal(theval){

  3. #3
    Join Date
    Oct 2012
    The problem I'm having is that I don't want the value changed on onchange but when the user clicks the Submit button.

    Also what is acting as the value? Is it the stuff between the option tags or is it the bit set by the value tag?

    i.e is it:

    <option value="fruitApple">Apples are nice</option>
    or is it:

    <option value="fruitApple">Apples are nice</option>
    In PHP you'd use the latter but it seems Javascript can use the former.

    Thanks again for your help.

  4. #4
    Join Date
    Nov 2010
    the value is "fruitApple". The text is "Apples are nice". If you want to pass the text to the function that would be a different code.

    Nothing actually changes on change - all that happens is that the function fires and the value gets passed to it. If you don't want that to happen, don't give it an onchange. As long as you give it a name and put it inside the form the value will be submitted when the form submits.

  5. #5
    Join Date
    Mar 2007
    value is determined by the value attribute if set.
    Always assign the value attribute or you'll get inconsistent results.

    The problem I'm having is that I don't want the value changed on onchange but when the user clicks the Submit button.
    Not quite sure what you mean by that, but you can use the onsubmit handler to read the value of the <select> and copy it presumably into a hidden-type element.

    <form onsubmit='this.notShown.value = this.mySelect.value'>
    <select name = 'mySelect'>
    <input type='hidden' name='notShown'>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  6. #6
    Join Date
    Oct 2012
    I think I've found out what I've been doing wrong.

    My code looked like this:

    oreName = form.oreName.selectedIndex
    oreName = form.oreName.options[oreName].text
    This gives me the text between the option open and close tags.

    However if I change my code to this:

    oreName = form.oreName.selectedIndex
    oreName = form.oreName.options[oreName].value
    Then I get the values set in the value="" part of the option.

    Now I come to the issue I had at the outset. What is better to use, if / elseif statements or switch and if the latter how would I correctly use it?

  7. #7
    Join Date
    Oct 2012
    Oh sweet I've worked out using switch. It all came down to accessing the values.

    Thanks for your help folks I truly do appreciate it.

  8. #8
    Join Date
    Nov 2010
    note that
    oreName = form.oreName.value
    will gove you the same result (but doesn't work with getting the text of the option)

  9. #9
    Join Date
    Oct 2012
    Oh right, so I can save more time on typing.

    I have to admit most of what I've been using has really been through stuff I've gleamed from various tutorials.

    I'm all for code simplification. :-)

  10. #10
    Join Date
    Nov 2010
    dude, if you want to save on typing you've got to get into this:
    <select id="myselect" onchange="showOpt(this)">
    <option value="0">Please select an option</option>
    <option value="red">Beans</option>
    <option value="blue">Jeans</option>
    function showOpt(sel) {
    alert("value is "+sel.value);
    alert("index is "+sel.selectedIndex);
    alert("id is "+sel.id)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14098 seconds
  • Memory Usage 2,940KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (9)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (10)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates