ravi patel
ravi patel

Reputation: 31

Need to Get the rid of many if's

I have variable in my java script which is global. Now i got different value each time when inner function call. I need to create a option tags with selected value as in attribute and one for without selected value based on the variable. I guess this is more confusing let me give you a example.

var a1 = "a1c" // default value but may change

if(a1 == "all")
{
    var allstatusdefault = '<option value="all" selected="selected">All</option>';
}
else
{
    var allstatusdefault = '<option value="all" >All</option>';
}

if(a1 == "a1b")
{
    var allstatusdefault1 = '<option value="a1b" selected="selected">a1b</option>';
}
else
{
    var allstatusdefault1 = '<option value="a1b" >a1b</option>';
}

if(a1 == "a1bc")
{
    var allstatusdefault2 = '<option value="a1bc" selected="selected">a1bc</option>';
}
else
{
    var allstatusdefault2 = '<option value="a1bc" >a1bc</option>';
}

This is just sample but i have to generate lot of option tag with different values.I don't want to write to many if ..anybody have any other idea?

Upvotes: 0

Views: 156

Answers (5)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 341003

Extract common code, I see a lot of duplication here.

var a1 = "a1c";

function buildOption(id) {
    var selected = (a1 == id? ' selected="selected"' : '');
    return '<option value="' + id + '"' + selected + '>' + id + '</option>';
}

var allstatusdefault =  buildOption('all');
var allstatusdefault1 = buildOption('a1b');
var allstatusdefault2 = buildOption('a1bc');

Upvotes: 3

Dhruvenkumar Shah
Dhruvenkumar Shah

Reputation: 528

I think from the way you are writing code you could do one with use jQuery. Create entire HTML first, with all the option values using jQuery object like this :

var $optionHTML = $('<option value=""> Blahblah </option>'); Now you can apply all jquery function to this guy. so you append a new option like this.

$optionHTML.append('<option>...</option>')

when you are done with all the option element use jquery selector method to find an element with option having value attribute matching to a1c then add attribute selected then you are done.

Do let me know if you need some code for starters.

Thanks.

EDIT :

HERE IS THE ANSWER

<html>
<head>
    <script type = "text/javascript" src= "http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script type = "text/javascript">
        $(document).ready (function () {
            var value = "test 2";
            $select = $("<select></select>");
            $optionHTML1 = $('<option value="test 1">Test 1</option>');
            $optionHTML2 = $('<option value="test 2">Test 2</option>');
            $optionHTML1.appendTo($select); 
            $optionHTML2.appendTo($select);
            $select.find("[value='test 2']").attr('selected','selected');
            alert($select.html());  
            $("div").append($select);
        });
    </script>
    <style type = "text/css">
    </style>
</head>
<body>
    <div>
    </div>
</body>

Upvotes: -1

Tschallacka
Tschallacka

Reputation: 28742

  var a1 = "a1bc" // default value but may change
    switch(a1)
        {
        case "all" : allstatusdefault = '<option value="all" selected="selected">All</option>';break;
        case "a1b" : allstatusdefault = '<option value="a1b" selected="selected">a1b</option>'; break;
        case "a1bc" :  allstatusdefault = '<option value="a1bc" selected="selected">a1bc</option>'; break;
        default :  allstatusdefault = '<option value="all" >All</option>';break;
        }
    window.alert(allstatusdefault); ​

Upvotes: 0

Ibu
Ibu

Reputation: 43850

From what i can deduct here is what you should do

var default1 = '<option value="'+a1+'" selected="selected">'+a1+'</option>';
var default2 = '<option value="'+a2+'" selected="selected">'+a2+'</option>';

Since the value of a1 is reused in the string, might as well just set it right away instead of using multiple if statements.

Note: when you have many if statement its the perfect opportunity to use a switch statement

Upvotes: 2

Some Guy
Some Guy

Reputation: 16210

For starters, learn about switch...case. In your case, it looks like you could possibly simply use the variable itself in the formation of the strings by concatenating the variable to a string.

Upvotes: 1

Related Questions