guradio
guradio

Reputation: 15555

append select set the value jquery

var data = [{'value': 'Select 1'},{'value': 'Select 2'},{'value': 'Select 3'}]


for(var i = 0 ; i > data.length; i++ ){

    var $row = $("<tr class=''><td > <center><select id='selectid' name='selectname' class='InputBox Select'><option value='Select 1'>Select 1</option><option value='Select 2'>Select 2</option><option value='Select 3'>Select 3</option><option value='Select 4'>Select 4</option></select><br> <span><small><i>Select</i></small></span></center></td><td></td><td></td></tr>");
    $("table#select").append($row);
    $('.Select option').filter(function () { return $(this).html() == data[i].value; console.log($(this).html+(data[i].value));} ).prop('selected', true);
    $('.Select option:selected').val(data[i].value);
    }

I generate a select from a jquery ajax success after i generate the select i want to put the value into the select. I tried using the common way like using val() but it is not working

Any suggestion is appreciated

Upvotes: 1

Views: 116

Answers (1)

kapantzak
kapantzak

Reputation: 11750

Try attr() function:

EDIT

$('.Select option')  
    .filter(function () { return $(this).html() == data[i].value } )
    .prop('selected', true)
    .attr('value', data[i].value);

Upvotes: 1

Related Questions