1

I have a post ajax that return retorno1 and just to get it simple i put their output as string value as ajax posted like the example .. then I need to add more than 1 option that the value is 0 so I need to say if the value is 0 show the selected item "NON" and if the value other so show it and select it.

I'm using setAttribute("selected", "selected") but I know it's wrong, so what is the correct code to add attribute to this string?

var i = 0;
var returno1 = "<option value='21'>Hello</option><option value='22'>Bye</option>";
var pre = retorno1 + '<option value="0">------ N/A ------</option>';
var count = $($.parseHTML(pre)).filter('option').length;
var dep_dr = $("#departamento_drop option:selected").val();

$.each($.parseHTML(pre),function(i,item){
    var val_drop  =($(item).val());
    var text_drop =($(item).html());

    if (val_drop == dep_dr){
        jQuery("#departamento_drop").html(pre).setAttribute("selected", "selected");
    }else if(dep_dr == "0"){
        jQuery("#departamento_drop").html(pre).setAttribute("selected", "selected");
    }
})
2
  • The entire posted code makes very little sense? What's the first i for? Why do you use $.parseHTML here? Why do you have a if/else if statement that does the same thing for both conditions etc. ? Commented Feb 10, 2016 at 15:08
  • I have a post ajax that return retorno1 with option tags .. then i need to add more than 1 option that the value is 0 só i need to say if the value is 0 show the selected item non and if the value is other so show it and select it. thanks Commented Feb 10, 2016 at 15:16

2 Answers 2

1

Working fiddle

Try to use attr() or prop() to set or get attribute to elements, check example bellow :

jQuery("#departamento_drop").empty();

$.each($.parseHTML(pre),function(i,item){
    var current_itme=$(item);
    var val_drop  =current_itme.val();
    var text_drop =current_itme.html();

    if (val_drop == dep_dr || dep_dr == "0"){
        current_itme=current_itme.attr("selected", "selected");
    }

    jQuery("#departamento_drop").append(current_itme);
})

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your help, but its not work correctly! its just select always the last item that its value 0
You're welcome, please describe a little more what you want to achieve.
@MinaMagdy check this fiddle that may help you.
0

Why do you need use strings? Use objects insted.

var values = [{val:33, title:'Hi'},{val:34, title:'Bue'},{val:0, title:'-------NA-------'}],
    selected = 34;
values.forEach(function(item){
    $('#dd').append($('<option>', {value:item.val, text:item.title, selected: item.val==selected}));
})

1 Comment

because its ajax return, i edited the post for give more details .. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.