1
$('#NameDropdown').change(function(){

                    $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: "http://localhost:8081/crownregency/getInfoUser.php",
                    data: {id: $('#NameDropdown').val(), checker: 1}, // 1 is to get user info
                    success:function(data){

                    $temp = data['Type'];
                    $get = $("#UserTypeDropdown option[value = '$temp']").text();
                    $('#UserType').attr('value', $get);
                }               
            });
        });

I have a problem with regard to placing the returned variable from ajax to the value. $get = $("#UserTypeDropdown option[value = '$temp']").text(); how do i solve this? pls help.. this question is connected to: jQuery get specific option tag text

Community
  • 1
  • 1
user1525703
  • 37
  • 1
  • 8

2 Answers2

2

Change this:

$get = $("#UserTypeDropdown option[value = '$temp']").text();

To:

$get = $("#UserTypeDropdown option[value='"+$temp+"']").text();

You could also use the filter method:

$("#UserTypeDropdown option").filter(function() {
     return this.value === $temp;
}).text();
Ram
  • 143,282
  • 16
  • 168
  • 197
  • @Vohuman thanks for solution, Is it possible to change a selector or find another way to avoid concatenating strings to make the selector. – Alireza Fattahi Mar 08 '15 at 05:38
  • @Vohuman +1 and thanks. To me this code seems much better! But considering the performance do you think the first approach is faster than using filter ?! – Alireza Fattahi Mar 09 '15 at 05:34
  • 1
    @AlirezaFattahi You are welcome! I prefer the `filter` method too. If performance is the key, jQuery should be avoided in the first place. You can select the `select` element and filter the matching `option` using a simple `for` loop. The performance differences of those code snippets are negligible. If you want to test them: http://jsperf.com – Ram Mar 09 '15 at 06:18
0

Maybe try this:

$get = $("#UserTypeDropdown option[value = '" + $temp + "']").text();
niaher
  • 9,460
  • 7
  • 67
  • 86