JZ.
JZ.

Reputation: 21907

How do I use jquery to set the values and text of a dropdown box from variables?

Say I have these variables:

  var  value1 = '';
  var textvalue1 = '';
...

How do I use jquery to set the values and text of a dropdown box from these variables?

 <div id"lang">
        <form name="languages" id="langSelector"> 
    <select name="file" size="1" target="_blank"> 
      <option value="VALUE">TEXT</option> 
      <option value=""></option>
      <option value=""></option>
      <option value=""></option>
      <option value=""></option>
    </select> 
  </form></div>

Upvotes: 0

Views: 198

Answers (2)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114437

$('#lang').find('option').eq(0).attr('value',0)
$('#lang').find('option').eq(0).attr('text','whatever')

adjust eq(0) to the position of the index you require.

Upvotes: 2

genesis
genesis

Reputation: 50982

$("option:eq(0)").val(textvalue1);

or you can use nt-child instead of eq

Upvotes: 1

Related Questions