Harshini
Harshini

Reputation: 73

display ajax response in div in ror

I am trying to display data from db using the value of a dropdown after a button is pressed through ajax. This is my ajax code:

$(function () {
  $("button").click(function(){
       var value2 = $('select#dropdown option:selected').val();

       $.ajax({
           type: "GET", 
           dataType: "json",
           url:"/mycontroller/action2",
           data: {data1: value2 }, 
           success:function(result1){
                 console.log(result1);
                 result1.forEach(function(){
                 $res_html = $("<div class= 'result1'>" + result1 + "</div>");
                 $('#test1').append($res_html);

                $("#test1").text(result1);

                  })
            }
      })
  });
});

I have a div:

<div id= "test1"></div>

my problem is i am unable to display data in div tag of view in ror.

Thanks in advance.

Upvotes: 0

Views: 1917

Answers (2)

Anurag_Soni
Anurag_Soni

Reputation: 542

add # in this line

$("test1").text(result1);

and use .html to append the html

 $(function()
    {
      $("button").click(function(){
            var value2 = $('select#dropdown option:selected').val();

       $.ajax({
            type: "GET", 
           dataType: "json",
           url: "/mycontroller/action2",
           data: { data1: value2 }, 
           success: function(result1){
                console.log(result1);
                var res_html = "";
                result1.forEach(function(){
                 res_html =+"<div class= 'result1'>" + result1 + "</div>";
                })
                 $("#test1").html(res_html);
            }
        })
  });
});

Upvotes: 1

Bud Damyanov
Bud Damyanov

Reputation: 31839

The proper syntax should be:

$("#test1").html(result1);

Upvotes: 1

Related Questions