Dinesh
Dinesh

Reputation: 1007

how to replace specfic value from the option tag using jquery

i want to append the text box values in the dropdown list when user can change the text box value that time the existing value should be replaced to changed value in the option tag without selected how i can do this

   var valuedata
   $('.txtbox1').click(function (){
       valuedata = $(this).val();
   });
   $('.txtbox1').focusout(function (){
        var data = $(this).val();
        if ($('.ddl [value ="' + valuedata + '"]').val() == valuedata) {
            alert(valuedata);    
            $('.ddl').append($("<option value=" + data + " ></option>").html(data).val(data));
        }
        else {
            $(".ddl [value='" + valuedata + "']").val(data);
        }
   });

Upvotes: 0

Views: 87

Answers (3)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85575

There are spaces before [value $(".ddl [value='" + valuedata + "']").val(data); which might be problem. So, remove that space:

$(".ddl[value='" + valuedata + "']").val(data);

Use html instead of append like this:

$('.ddl').html($("<option value=" + data + " ></option>")

Upvotes: 0

Nish
Nish

Reputation: 325

Try this :

$('select option:contains("old_text")').text('new_text');

To check for option is appended or not :

$("select option").each(function(i,obj){
    if($(obj).text() == "your_text"){
        console.log("yes");
    }
})

Upvotes: 1

Awlad Liton
Awlad Liton

Reputation: 9351

DEMO: http://jsfiddle.net/c2Ry8/

    $(document).ready(function() {
        $('.txtbox1').focusout(function (){
            var data = $(this).val();
            $('.ddl').append("<option value=" + data + " >"+data+"</option>");
            });
    });

Upvotes: 0

Related Questions