0

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.

Anus G
  • 15
  • 7
  • Set the `value` after appending `options` because when you are setting the `value`, there is no option in the select input to be selected... – Rayon Aug 26 '16 at 11:48
  • you have to write `$('[name=charge]').val( 'Funds' );` within ajax success handler where data is getting attached to select box. – vijayP Aug 26 '16 at 11:49
  • probably duplicate of http://stackoverflow.com/questions/3518002/how-can-i-set-the-default-value-for-an-html-select-element – Tyler Aug 26 '16 at 11:50
  • `select` requires closing tag. – itzmukeshy7 Aug 26 '16 at 12:00

3 Answers3

2

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>
Ehsan
  • 12,655
  • 3
  • 25
  • 44
0
$(document).ready(function(){

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

});

the use ajax data to append to same id

Akshay
  • 815
  • 7
  • 16
0

$(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>

    
    
  
Roma
  • 272
  • 1
  • 12