1

what I am trying to do is append a <select> tag and do an if statement.

I do have error. I think it's the way I am concatenating the string..

here's my code:

var load_volumes = jQuery('.micronic').val();
var arr_volumes = load_volumes.split(',');

var count_volumes = arr_volumes.length;
for(x=0; x<count_volumes; x++){

    jQuery('.append_here').append("<select class='volumes'>"
                                    +"<option>Choose Volume</option>"
                                    +"<option"
                                    + if(arr_volumes[x] == 0.5){
                                    + "selected"    
                                    + }
                                    +">0.5</option>"
                                    +"<option>0.75</option>"
                                    +"<option>1.10</option>"
                                    +"<option>1.40</option>"
                                    +"<option>2.0</option>"
                                    +"<option>2.5</option>"
                                    +"<option>3.0</option>"
                                    +"<option>4.0</option>"
                                    +"<option>6.0</option>"
                                    +"<option>7.5</option></select> &nbsp");
}

thanks in advance.

9
  • 1
    What's the error you are getting? Commented Jun 29, 2015 at 11:35
  • here's the error Uncaught SyntaxError: Unexpected token if @PraveenKumar Commented Jun 29, 2015 at 11:36
  • You'd do well to add that crucial information to the question itself. Commented Jun 29, 2015 at 11:36
  • 2
    You can't have an if statement in the middle of a string like that Commented Jun 29, 2015 at 11:36
  • 1
    + (arr_volumes[x] == 0.5 ? "selected" : "") Commented Jun 29, 2015 at 11:38

4 Answers 4

3

See my answer below. You should consider using a template engine such as handlebars though.

jQuery('.append_here').append("<select class='volumes'>"
                                +"<option>Choose Volume</option>"
                                +"<option"
                                + (arr_volumes[x] == 0.5? " selected" : "")
                                +">0.5</option>"
                                +"<option>0.75</option>"
                                +"<option>1.10</option>"
                                +"<option>1.40</option>"
                                +"<option>2.0</option>"
                                +"<option>2.5</option>"
                                +"<option>3.0</option>"
                                +"<option>4.0</option>"
                                +"<option>6.0</option>"
                                +"<option>7.5</option></select> &nbsp");
Sign up to request clarification or add additional context in comments.

1 Comment

the use of ternary if else is nice too.. thanks again. =)
1

Replace the line with if with ternary selector:

+ (arr_volumes[x] == 0.5? " selected" : "")

Comments

1

The append function should have a single element as an input, but this would produce only warnings. The problem in your code is

+ if(arr_volumes[x] == 0.5){
+ "selected"    
+ }

Ideally set this value in advance. Your code could look something like this:

var volumes = (arr_volumes[x] == 0.5) ? " selected" : ""; 
var select = "<select class='volumes'>"
                            +"<option>Choose Volume</option>"
                            +"<option"
                            + volumes
                            +">0.5</option>"
                            +"<option>0.75</option>"
                            +"<option>1.10</option>"
                            +"<option>1.40</option>"
                            +"<option>2.0</option>"
                            +"<option>2.5</option>"
                            +"<option>3.0</option>"
                            +"<option>4.0</option>"
                            +"<option>6.0</option>"
                            +"<option>7.5</option></select> &nbsp";
jQuery('.append_here').append(select);

But consider something like this in the end to make it variable:

var values = [0.5, 0.75, 1.10, 1.40, 2.0, 2.5, 3.0, 4.0, 6.0, 7.5];
var content = "<select class='volumes'>"
for(var i=0; values.length; i++) {
    content += "<option";
    content += (arr_volumes[x] == values[i]) ? " selected" : "";
    content += ">" + values[i] + "</option>";
}
content += "</select>";

Comments

0

Try this way to select

var s="";
if(arr_volumes[x] == 0.5){
         s=" selected "  ;
}

jQuery('.append_here').append("<select class='volumes'>"
                                    +"<option>Choose Volume</option>"
                                    +"<option"
                                    + s 
                                    +">0.5</option>"
                                    +"<option>0.75</option>"
                                    +"<option>1.10</option>"
                                    +"<option>1.40</option>"
                                    +"<option>2.0</option>"
                                    +"<option>2.5</option>"
                                    +"<option>3.0</option>"
                                    +"<option>4.0</option>"
                                    +"<option>6.0</option>"
                                    +"<option>7.5</option></select> &nbsp");

1 Comment

There is no space after option so string will be optionselected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.