Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better way to do if statment?

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
I was wondeirng if there was a better way to do an if statement.

I am looping thorugh an array, and I want to check to see if the current value is equal to a certain string. Here is my code:

Code:
var advancedSearchTable = document.createElement("table");

var labels = new Array('Modifier', 'Class', 'Title', 'Author', 'External');

for (var i = 0; i < labels.length; i++) {
    // Create a table row
    var row = advancedSearchTable.insertRow();
			
    // This cell will hold the label
    var label = row.insertCell();
			
    // Style the label
    label.style.fontSize = labelFontSize;
    label.style.fontWeight = labelBoldness;
    label.style.labelAlign = labelAlign;
    label.style.padding = labelPadding;
    label.innerHTML = labels[i];
			
    // This cell will hold the form component
    var field = row.insertCell();	
    field.id = 'advancedSearch' + labels[i];
    field.style.textAlign = 'right';
			
    if (labels[i] == ('Title' || 'Author')) {
        // Create a text box
        var formComponent = document.createElement("input");
        formComponent.type = 'text';
				
    } else if (labels[i] == ('Modifier' || 'Class')) {
        // Create a text box
        var formComponent = document.createElement("select");
			
    } else {
        // Create a text box
        var formComponent = document.createElement("input");
        formComponent.type = 'checkbox';
    }
			
    field.appendChild(formComponent);
}

Currently it doesnt seem to work for 'Author' or 'Class'

Thanks!
 
Ah, simple change and you are there...
Change the code to...
Code:
if (labels[i] == 'Title' || labels[i] == 'Author')) {
And...
[/code]} else if (labels == 'Modifier' || labels == 'Class')) {[/code]
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
You can't do this:

Code:
if (labels[i] == ('Title' || 'Author')) {

You have to split the tests:

Code:
if (labels[i] == 'Title' || labels[i] == 'Author') {

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top