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.
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();
});
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 ))
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.
Bookmarks