View Full Version : Javascript - Switching DIV's

23 Jul 2010, 04:05 PM
Hi, just followed a tutorial which showed how to use javascript to switch between divs.

Script seems to work fine, but theres a part i dont understand.

function ShowContent(d,e) {
if (d.length < 1) {return;} - Not sure what this line here does?
document.getElementById(d).style.display = "block";
document.getElementById(e).style.display = "none";

onclick="ShowContent('one','two'); return true;"
Page 1

Anyone care to enlighten me? ;)

24 Jul 2010, 04:16 AM
If the length of the string (which is the variable "d" in your example) being passed in is less than 1 character long (in other words empty), return.

"return" immediately stops the function it's inside of and returns the value after it. In the case of no value, it simply stops the function.


function ShowMessage() {
alert('Hi There');
The above example will never alert anything when you call ShowMessage() because it returns before you reach the alert;

function ShowMessage() {
alert('Hi There');
alert('I love Ice Cream');
The above example will alert "Hi There", but not "I love Ice Cream".


if (d.length < 1) {return;}

means the same thing as:

if (d.length < 1) {

Hope this answers your question.