Forum Moderators: open
Can anyone tell me if I need to add braces anywhere to this to make it work.
function showdiv(divname)
{
if (divname == "sddiv" )
if (document.foo.sd.checked)
document.foo.sddiv.style.visibility='visible';
else
document.foo.sddiv.style.visibility='hidden';
else if (divname == "lddiv")
if (document.foo.ld.checked)
document.foo.lddiv.style.visibility='visible';
else
document.foo.lddiv.style.visibility='hidden';
}
Thanks, -- Dave
function showdiv(divname)
{
if (divname == "sddiv" )
{
if (document.foo.sd.checked)
{
document.foo.sddiv.style.visibility='visible';
}
else
{
document.foo.sddiv.style.visibility='hidden';
}
}
else if (divname == "lddiv")
{
if (document.foo.ld.checked)
{
document.foo.lddiv.style.visibility='visible';
}
else
{
document.foo.lddiv.style.visibility='hidden';
}
}
}
the sytnax is this:
var x = (condition)? 'value if true' : 'value if false';
function showdiv(divname) {
if (divname == "sddiv" ) {
document.foo.sddiv.style.visibility = (document.foo.sd.checked)? 'visible' : 'hidden';
} else if (divname == "lddiv") {
document.foo.lddiv.style.visibility = (document.foo.ld.checked)? 'visible' : 'hidden';
}
}
OR (depending on your application and naming convention)
function showdiv(divname) {
var cb = divname.substr(0,2);//first 2 char
document.foo[divname].style.visibility = (document.foo[cb].checked)? 'visible' : 'hidden';
}
- JS
[endeavorpub.com...]
function showdiv(divname) {
var cb = divname.substr(0,2);//first 2 char
document.foo[divname].style.visibility = (document.foo[cb].checked)? 'visible' : 'hidden';
}
There is not a "." between "foo" and "[divname] or between "foo" and "[cb]"?
BTW, I've got two books on Javascript: 'JavaScript - The Definitive Guide' by David Flanagan (O'Reilly) && 'Beginning JavaScript' by Paul Wilton (Wrox). I found the first way over my head and the second only marginally helpful. How is the best way (or what is the best book) to learn JavaScript?
Thanks,
Dave
In your code:
function showdiv(divname) {
var cb = divname.substr(0,2);//first 2 char
document.foo[divname].style.visibility = (document.foo[cb].checked)? 'visible' : 'hidden';
}There is not a "." between "foo" and "[divname] or between "foo" and "[cb]"?
That is correct. There are 2 ways to access any object property in javascript. Either
var obj = new Object();
obj.prop = "Hello World";
alert(obj.prop);//alerts Hello World
alert(obj["prop"]);//alerts Hello World
var strVariable = "pro";
alert(obj[strVariable+"p"]);//alerts Hello World
Now why would you want to use the [] notation? Consider this:
var obj = new Object();
obj.prop1 = "Prop 1";
obj.prop2 = "Prop 2";
obj.prop3 = "Prop 3";
obj.prop4 = "This is Prop 4";
//alerts Prop 1, then Prop 2...
for (i = 0; i <= 4; i++) {
alert(obj["prop"+i]);
}
This allows you to use variables to access properties.
Also consider this:
for (propName in obj) {
//alerts "prop1 = Prop 1",then "prop2 = Prop 2"...
alert(propName+" = "+obj[propName]);
}
BTW, I've got two books on Javascript: 'JavaScript - The Definitive Guide' by David Flanagan (O'Reilly) && 'Beginning JavaScript' by Paul Wilton (Wrox). I found the first way over my head and the second only marginally helpful. How is the best way (or what is the best book) to learn JavaScript?Thanks,
Dave
When I first started learning javascript, I read a few books(also way over my head), copied/modified scripts from the internet, and then just played around. I really started learning alot when I started
1. using the Firebug extention for Firefox 1.5
- this allows you to "browse" the entire Document Object Model
2. lots of lurking/posting in forums/newsgroups. alt.lang.javascript in Google Groups is one of my favorites. (beware, everyone is very arrogant about their vast JS knowledge, but they will still help). Read through the FAQ of that group and you will learn a lot.
- JS