0

e.g.I want to add 'selected' attribute at the third position. Can you explain if I can add selected attribute in append() of jquery.

for (i = 0; i < 10; i++)
{ 
     if(i==3)
     {
       $('#mySelect').append($('<option>',
       {
          value: i,
          text :i
      }));
    }
}
1
  • Please considering accept Rory McCrossan's answer and my apologize for the misleading. Commented May 4, 2018 at 14:35

3 Answers 3

2

The issue is because of your nested if statement, which results in only the option with a value of 3 being appended to the select.

To fix this you can append all the option elements together, then set the val() of select to choose the specific one you want. Try this:

var html = [];
for (i = 0; i < 10; i++) {
  html.push($('<option>', {
    value: i,
    text: i
  }));
}

$('#mySelect').append(html).val(3);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="mySelect"></select>

Sign up to request clarification or add additional context in comments.

Comments

1

Edit: Correct the answer as per Rory McCrossan's comment(added append() with jQuery object), and apologize for misleading.

The issue is about the if statement, your code would only append one <option> that is if i equals 3.

jQuery API Documetation

According to the documentation, you have several options to do so...

1) Use append() with jQuery object

for (var i = 0; i < 10; i++) {
  $('#mySelect').append($('<option>', {
    value: i,
    text: i,
    selected: i == 3 ? true : false
  }))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>

2) Use appendTo()

for (var i = 0; i < 10; i++) {
  $('<option>', {
    value: i,
    text: i,
    selected: i == 3 ? true : false
  }).appendTo($('#mySelect'))
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>

3) Use append() with a method

for (var i = 0; i < 10; i++) {
  $('#mySelect').append(CreateOption(i))
}

function CreateOption(i) {
  return $('<option>', {
    value: i,
    text: i,
    selected: i == 3 ? true : false
  })
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>

4) Use append() with text

for (var i = 0; i < 10; i++) {
  var selected = i==3 ?  ' selected=true' : ''
  var option = '<option value='+ i + selected + '>' + i + '</option>'
  $('#mySelect').append(option)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="mySelect"></select>

2 Comments

So you can't append an element like that Yes you can. The documentation even says so, but you conveniently missed out that part. Content: DOM element, text node, array of elements and text nodes, HTML string, or **jQuery object** to insert. Your second method is event doing this, just in a more obfuscated manner. In fact, I'm not sure how this is an accepted answer as what the OP has already works. This is just a regurgitation of the documentation. Please consider the accuracy of your answers before potentially mis-leading new visitors.
@RoryMcCrossan You're right, seems I have misleading some of the users and visitors, I'll correct the answer and please take a look if there's anything wrong. Thanks for your point out and apologize for the mistake I done.
1

How about

$('#mySelect :nth-child(3)').attr('selected', 'selected');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.