www.webdeveloper.com
Results 1 to 4 of 4

Thread: have hidden text appear when clicking a button, or selecting text, or check box, etc

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    15

    have hidden text appear when clicking a button, or selecting text, or check box, etc

    hi

    i have this text, a question:
    (visible text)

    "1. if so and so does x then

    A. answer A here
    B. answer B here
    C. answer C here
    D. answer D here"

    then what i want is when the user selects one of the answers, the answer, which is hidden will appear beneath the question

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,931

    Lightbulb

    One possible solution ...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <meta charset="utf-8">
    <style type="text/css">
     li { list-style-type:none; text-indent:2em; line-spacing:1.5em; }
    .answer { color:red; }
    </style>
    
    </head>
    <body>
    I have this text, a question:
    (visible text)
    
    <div id="review">
     <ul> <b>1. if so and so does x then:</b> <br>
      <li> <input type="radio" name="q1" value='a1' onclick="showAnswer(this.value)">
           A. y is the dependent variable
           <div id='a1' class="answer">answer A here<p></div>
      </li>
      <li> <input type="radio" name="q1" value='b1' onclick="showAnswer(this.value)">
           B. y is another independent variable
           <div id='b1' class="answer">answer B here<p></div>
      </li>
      <li> <input type="radio" name="q1" value='c1' onclick="showAnswer(this.value)">
           C. a<sup>2</sup> is a constant
           <div id='c1' class="answer">answer C here<p></div>
      </li>
      <li> <input type="radio" name="q1" value='d1' onclick="showAnswer(this.value)">
           D. b<sup>2</sup> is a variable
           <div id='d1' class="answer">answer D here<p></div>
      </li>
      <p>
     </ul>
    
     <ul> <b>2. if this and that does these then:</b> <br>
      <li> <input type="radio" name="q1" value='a2' onclick="showAnswer(this.value)">
           A. y is the dependent variable
           <div id='a2' class="answer">answer A here<p></div>
      </li>
      <li> <input type="radio" name="q1" value='b2' onclick="showAnswer(this.value)">
           B. y is another independent variable
           <div id='b2' class="answer">answer B here<p></div>
      </li>
      <li> <input type="radio" name="q1" value='c2' onclick="showAnswer(this.value)">
           C. a<sup>2</sup> is a constant
           <div id='c2' class="answer">answer C here<p></div>
      </li>
      <li> <input type="radio" name="q1" value='d2' onclick="showAnswer(this.value)">
           D. b<sup>2</sup> is a variable
           <div id='d2' class="answer">answer D here<p></div>
      </li>
      <p>
     </ul>
    
    </div>
    
    <script type="text/javascript">
    function showAnswer(info) {
      document.getElementById(info).style.display = 'block';
    }
    
    window.onload = function() {
      var sel = document.getElementById('review').getElementsByTagName('div');
      for (var i=0; i<sel.length; i++) { sel[i].style.display = 'none'; }
    }
    </script>
    
    </body>
    </html>

  3. #3
    Join Date
    Apr 2011
    Posts
    15
    thanks.

    i went with something similar

    <head>

    <script type="text/javascript">
    function toggle(element) {
    document.getElementById(element).style.display = (document.getElementById(element).style.display == "none") ? "" : "none";
    }
    </script>

    </head>

    <body>

    <a href="javascript:toggle('text')">See text </a><div id="text" style="display: none;">
    Text to see here

    </div>

    ---

    thanks for your reply. i like the radio button style

  4. #4
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,931

    Lightbulb

    You're most welcome.
    Happy to help.

    Note, your version will still require unique 'id' values.
    Although it legal, it's not a good idea to use reserved words (text) as 'id' values.
    Also, do not need to use the 'javascript:' prefix in the href= anchor tag.
    And, you could substitute 'inline' for 'block' if you do not use the '' style setting in the toggle function

    For example:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Untitled </title>
    
    <style type="text/css">
    .answers { display:none; }
    </style>
    
    </head>
    <body>
    
    <a href="#" onclick="return toggle('txt')">See text</a><br>
    <div id="txt" class="answers">Text to see here</div>
    
    <a href="#" onclick="return toggle('txt1')">See another text</a><br>
    <div id="txt1" class="answers">More text to see here</div>
    
    <script type="text/javascript">
    function toggle(el) {
      document.getElementById(el).style.display
        = (document.getElementById(el).style.display == "none") ? "block" : "none";
    // could substitute 'inline' for 'block' in above line
      return false;
    }
    window.onload = function() {
      var sel = document.getElementsByClassName('answers');
      for (var i=0; i<sel.length; i++) { sel[i].style.display = 'none'; }
    }
    </script>
    
    </body>
    </html>

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.17430 seconds
  • Memory Usage 2,985KB
  • Queries Executed 13 (?)
More Information
Template Usage (30):
  • (1)SHOWTHREAD
  • (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
  • (2)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./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
  • ./includes/functions_threadedmode.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 (71):
  • 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_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_threaded
  • showthread_threaded_construct_link
  • 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