views:

293

answers:

3

I am building a conditional navigation menu that has 3 levels (the 3rd level added today by business, no worries its not like I launch next week. Oh wait I do:)). I have a javascript var that contains the html for my first conditional level. I am now trying to insert another level inside of the first.

var myVar = '<ul class="linksUnit">';
var myVar += '<li>Link 1</li>';
var myVar += if (myVar2 != false) {document.write("Link 2")};
var myVar += '</ul>';

Any help would be greatly appreciated.

Thanks

+9  A: 

You should write that this way (knowing little javascript myself, but i think this is right):

var myVar  = '<ul class="linksUnit">';
myVar     += '<li>Link 1';
if (myVar2) { // note != false means true
    // insert a nested unordered list
    myVar += '<ul>';
    myVar += '<li>Link 2</li>';
    myVar += '</ul>';
}
myVar     += '</li>'; 
myVar     += '</ul>';

var is only needed the first time you declare the variable.

W3C DOM

If you build up the string that you append / insert into your document like that, it will soon become quite confusing. For example, if you want to change something, you have to fiddle with the strings. Better you use the W3C DOM API, which is standardized and provides a clean way to build up a element tree which can be appended to any child element into the document tree. Here you will find a nice introduction into the matter: W3C DOM Introduction. After you read this, you can start looking at the methods of W3C DOM. Here is a good reference: DOM2 Reference. Start with document.createElement and work your way through.

Johannes Schaub - litb
For the sake of the sanity of yourself and whatever poor maintenance programmer comes after you, please actually create the DOM objects you want instead of using strings for them.
rmeador
This guy admitted not really knowing JavaScript, don't be too hard on the novice...
Jason Bunting
thanks jason. at least one guy feeling the pain in me :)
Johannes Schaub - litb
+6  A: 

var myVar += (myVar2 != false ? "Link 2" : "");

Ternary conditional operator etc.

Thom
myVar += (myVar2 ? "Link 2" : "");
some
What's with the "!= false" - not necessary!
J-P
A: 

For the record, I don't agree with the negative rating that was here when I first read question--from what I understand, the point of this site is to have the community answer very specific questions about development, which is exactly what this is. I'm sure the answers to the questions Joel Spolsky had in mind when he and Jeff came up with the idea could have easily been prefixed with "duh" by a number of people, but that nevertheless does not mean he could have used a quick answer. I think treating a question like this as somehow invalid is completely off-putting to the types of people the community is trying to foster.

Marc Bollinger
Your opinion is valid. However, it does not belong here as an "answer" to the OP's question and should be deleted. Stack Overflow is not a discussion forum or message board. Please use the "Comment" feature for things like this.
Jason Bunting