Q: How to pass a javascript string to javascript then convert output to string to pass to function I can not get the ' and " to properly escape in my javascript. Here is my code:
<script>
//live search
    $(document).ready(function(){
        $("#livebox").on("input",function(e){
            $("#datalist").empty();
            $.ajax({
                method:"post",
                url:"/communitysearch",
                data:{text:$("#livebox").val()},
                success:function(res){
                    var data = "<div class='list-group'>";
                    $.each(res,function(index,value){
                        //This part can not deal with the ' and "
                        data += "<a href='#' class='list-group-item' onclick='select("+value.id+", '"+value.name+"')>"+value.name+"</a>";
                    });
                    data += "</div>";
                    $("#datalist").html(data);
                }
            });
        });
    });

    function select(id, name) {
        alert('in select')
        $("#datalist").empty();
    }
</script>

Here is the output for the line I'm talking about:
<a href="#" class="list-group-item" onclick="select(1, " first')="">first</a>

I need it to do this to work:
<a href="#" class="list-group-item" onclick="select(1, "first")">first</a>


A: data += "<a href='#' class='list-group-item' onclick='select(" + value.id + ", \"" + value.name + "\")>" + value.name + "</a>";

Example:
>>> value.id = '1'
>>> value.name = 'name'
>>> data = ''
>>> data += "<a href='#' class='list-group-item' onclick='select(" + value.id + ", \"" + value.name + "\")>" + value.name + "</a>";
>>> data
'<a href=\'#\' class=\'list-group-item\' onclick=\'select(1, "name")>name</a>'
>>> print(data)
<a href='#' class='list-group-item' onclick='select(1, "name")>name</a>


A: Thanks for the tip on " to escape out. Eventually I got it to work with data written this way:
data += "<a href='#' class='list-group-item' onclick='select(" + value.id + ", \"" + myname + "\")'>" + myname + "</a>";

