www.webdeveloper.com
Results 1 to 8 of 8

Thread: Trying to target table id with jQuery... what am i doing wrong?

  1. #1
    Join Date
    May 2012
    Posts
    84

    Trying to target table id with jQuery... what am i doing wrong?

    Hello All,

    I am working on a project that consists of jQuery, JSON, and traditional JavaScript syntax.

    I would like to have jQuery make a table visible when a user selects a specific region otherwise it should remain invisible. My code is working somewhat I am just having trouble targeting the selector in jQuery.

    I believe this line
    Code:
     document.writeln("<table id= \"north\" border=\"0\" width=\"100\">");
    is written incorrect therefore when i attempt target (table #north) with jQuery, jQuery is unable to find the "id" tag.

    Any suggestions are welcome.

    Thank you for your time and efforts in advance.

    Code:
    $(function(){
             $("table #north").hide();
    		
    			$("#region").change(function(){
    				if($("#region").val()=="3"){
    				
    					$("table #north").show();
    				}else{
    					$("table #north").hide();
    				
    				}
    			});
    		
    		});
    JavaScript code:
    Code:
    <script type="text/javascript">
    			var i;
    			
    			document.writeln("<table id= \"north\" border=\"0\" width=\"100\">"); <=== I believe the problem is here?
    
    	for(i=0;i<Hoes.North.length;i++) //retrieve North-East Hoes
    	{
    		document.writeln("<td>");
    		document.writeln("<tr><td><b>Hoe_Name:<b></td><td width=50>"+Hoes.North[i].Name+"</td></tr>"),
    		document.writeln("<tr><td><b>Hoe_Age:<b></td><td width=50>"+Hoes.North[i].age+"</td></tr>"),
    		document.writeln("<tr><td><b>Location:<b></td><td width=50>"+Hoes.North[i].location+"</td></tr>");
    	}
    			document.writeln("</td></table><br>");

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,716
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>...</title>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    var Hoes_North=[
    /* 'name', 'age', 'location' */
    ['aaa','1','somewhere'],
    ['bbb','2','somewhere'],
    ['ccc','3','somewhere']
    ];
    
    document.writeln("<table align='center' id='north' border='0' width='100'>");
    for(var i=0;i<Hoes_North.length;i++){
    document.writeln("<tr><td><b>Hoe_Name:<b></td><td width='50'>"+Hoes_North[i][0]+"</td></tr>");
    document.writeln("<tr><td><b>Hoe_Age:<b></td><td width='50'>"+Hoes_North[i][1]+"</td></tr>");
    document.writeln("<tr><td><b>Location:<b></td><td width='50'>"+Hoes_North[i][2]+"</td></tr>");
    }
    document.writeln("</table><br>");
    
    $(document).ready(function(){
    $("table#north").hide();
    $("#region").change(function(){
    if($("#region").val()=="3"){$("table#north").show();}
    else{$("table#north").hide();}
    $(this).blur();
    });
    });
    </script>
    </head>
    <body>
    <center>
    <select id="region"><option value="">choose</option><option value="3">North</option><option value="2">South</option></select>
    </center>
    </body>
    </html>
    signature under construction

  3. #3
    Join Date
    May 2012
    Posts
    84

    It Worked!

    Hi Padonak! Thank you for your help. It is now working perfect! I decided to go with a switch statement to optimism the code a bit.

    Code:
    $("#region").change(function(){
    		
    		switch(true){
    			case($(this).val()=="1"):
    				$("table#north").show();
    				$('table#south, table#west').hide();
    				break;
    			case($(this).val()=="2"):
    				$("table#south").show();
    				$('table#north, table#west').hide();
    				break;
    			case($(this).val()=="3"):
    				$("table#west").show();
    				$('table#north, table#south').hide();
    				break;
    			default:
    		}
    			});

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,716
    one more (paranoid lol):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>...</title>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    var hoes=[
    /* 'id', 'name', 'age', 'location' */
    ['north','aaa','1','somewhere'],
    ['north','bbb','2','somewhere'],
    ['north','ccc','3','somewhere'],
    
    ['south','ddd','11','somewhere'],
    ['south','eee','22','somewhere'],
    ['south','fff','33','somewhere'],
    
    ['west','kkk','44','somewhere'],
    ['west','lll','55','somewhere'],
    ['west','mmm','66','somewhere']
    ];
    
    var curr='';
    document.writeln("<center>");
    for(var i in hoes){
    if(i==0){curr=hoes[i][0];document.writeln("<table align='center' id='"+curr+"' border='1' width='100' style='display:none'>");}
    if(i>0&&hoes[i][0]!=curr){
    curr=hoes[i][0];
    document.writeln("</table>");
    document.writeln("<table align='center' id='"+curr+"' border='1' width='100' style='display:none'>");
    }
    document.writeln("<tr><td><b>Hoe_Name:<b></td><td width='50'>"+hoes[i][1]+"</td></tr>");
    document.writeln("<tr><td><b>Hoe_Age:<b></td><td>"+hoes[i][2]+"</td></tr>");
    document.writeln("<tr><td><b>Location:<b></td><td>"+hoes[i][3]+"</td></tr>");
    }
    document.writeln("</table>");
    curr='';
    document.writeln("<select id='region'><option value=''>choose</option>");
    for(var k in hoes){
    if(hoes[k][0]!=curr){
    curr=hoes[k][0];
    document.writeln("<option value='"+curr+"'>"+(curr.substring(0,1).toUpperCase()+curr.substring(1,curr.length))+"</option>");
    }
    }
    document.writeln("</select></center>");
    
    
    $(document).ready(function(){
    $("#region").change(function(){
    var val=$(this).val(),opts=this.options;
    if(val!=""){
    for(var i in opts){
    var opval=opts[i].value;
    if(opval==val || opval==''){continue;}
    else{$("#"+opval).hide();}
    }
    $("#"+val).show();
    }
    $(this).blur();
    });
    });
    </script>
    </head>
    <body></body>
    </html>
    signature under construction

  5. #5
    Join Date
    May 2012
    Posts
    84

    Sweet!!!

    Awesome! When you have a sec if you don't mind can explain the code a little bit? I am trying to become proficient with javascript and frameworks.

    Also, can you recommend any additional websites, blogs, books etc focused on javascript and frameworks api development?

    Thanks again Padonak.

    Code:
    $(document).ready(function(){
    $("#region").change(function(){
    var val=$(this).val(),opts=this.options;
    if(val!=""){
    for(var i in opts){
    var opval=opts[i].value;
    if(opval==val || opval==''){continue;}
    else{$("#"+opval).hide();}
    }
    $("#"+val).show();
    }
    $(this).blur();
    });

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,716
    prepare yourself for terrible english in my comments

    Code:
    $(document).ready(function(){// Specify a function to execute when the DOM is fully loaded
    
    $("#region").change(function(){// when we change SELECT
    
    var val=$(this).val(), // shorthand for the selected option value
    
    opts=this.options; // this is array which contains all the options of this SELECT
    
    if(val!=""){// if this is not the first option (which has no value and is just a header)
    
    for(var i in opts){// we start a loop
    
    var opval=opts[i].value;// shorthand for the current option value (current in the loop)
    
    if(opval==val || opval==''){continue;}// if the current in the loop option is "choose" which has no value or if this is the selected one - the loop skips this index. that's because we are looking for table id's to hide.
    
    else{$("#"+opval).hide();}// this option is not the selected one and not "choose". that means this value contains id of a table to hide
    }
    
    $("#"+val).show();// after the loop stops we show the proper table
    }
    
    $(this).blur();// this removes focus from SELECT
    });
    });
    i don't think i can recommend you websites. i am not a prof coder, scripting is just a hobby for me. and i love this forum ))

    as for books, i can share JQuery reference if you need it.
    signature under construction

  7. #7
    Join Date
    May 2012
    Posts
    84
    Padonak Thanks so much for all your help! I am going to store this in my js library for future reference. I have a lot to learn but i am having fun doing it.

  8. #8
    Join Date
    May 2012
    Posts
    8
    Hey sorry for commenting but, moderators can you accept my post?

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.23895 seconds
  • Memory Usage 2,930KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (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
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (8)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (8)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)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 (73):
  • 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
  • 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