Forum Moderators: open

Message Too Old, No Replies

Nested IF controls - Syntax

Javascript Syntax qestion

         

user404

9:29 pm on May 8, 2006 (gmt 0)

10+ Year Member



Hi I'm extremely new to Javascript.

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';
}

I'm sure there's a cool way of doing the same thing with less code, but this is my best attempt with what I know.

Thanks, -- Dave

Fotiman

9:59 pm on May 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't be afraid of braces. The extra characters are well worth it and make your code much more readable. I think your code, as it is now, is probably invalid (the 2nd else statement might not work). Here's how it should look:


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';
}
}
}

jshanman

2:58 pm on May 9, 2006 (gmt 0)

10+ Year Member



I was originally scared of doing anything but using tons of brackets, but once I learned the syntax of the shorthand, it works great!

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...]

user404

4:28 am on May 10, 2006 (gmt 0)

10+ Year Member



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]"?

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

jshanman

12:48 pm on May 10, 2006 (gmt 0)

10+ Year Member



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