Anus G
Anus G

Reputation: 15

set default select values from drop down

I need to make a default select value from the drop down where the data is dynamically generated.

<html>
<tr><td><Select id="charge" name="charge"/></td></tr>
</html>

I had used so ajax call to get the data and populated the list in to the id:charge. Now from the list i need to keep one values as a default select . Can any one help me .

$(document).ready(function(){

 $('[name=charge]').val( 'Funds' );

});

but it does not work.

Upvotes: 0

Views: 731

Answers (3)

Ehsan
Ehsan

Reputation: 12959

Use this code :

$(document).ready(function(){

$('[name=charge]').append("<option selected>Funds</option>")

})

Final code :

<html>
<head>
</head>
<body>
    <tr>
        <td>
            <Select id="charge" name="charge"/>
        </td>
    </tr>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
        
    $(document).ready(function(){

        $('[name=charge]').append("<option selected>Funds</option>")

    })
    
    </script>
</body>
</html>

Upvotes: 2

Roma
Roma

Reputation: 282

$(document).ready(function(){
$('[name=charge]').append("<option>Funds1</option>");
$('[name=charge]').append("<option selected='selected'>Funds</option>");
$('[name=charge]').append("<option>Funds2</option>");

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    
  <tr><td><Select id="charge" name="charge"/>
        </td></tr>

    
    
  

Upvotes: 0

Akshay
Akshay

Reputation: 805

$(document).ready(function(){

 $('#charge').append('<option value="funds">option1</option>');

});

the use ajax data to append to same id

Upvotes: 0

Related Questions