dannymitza
dannymitza

Reputation: 59

Ajax returned JSON striping space

I have an AJAX request that process a JSON array, but when I try to use it's value, everything that's after a space is deleted.

Here's the JavaScript code:

$(document).ready(function(){
$("#json").on('keyup', function() {
    var url = "http://localhost:8080/admin/ajax/";
    $.ajax({
        url:url + $(this).val(),
        async:true,
        data:{},
        dataType:"JSON",
        success: function(data, status){
            var output = "<ul>";
            $.each(data, function(index, key){
                var k = key.nume;
                output += "<li><a href='#' id='damus' data-target='" + k.trim() + "'>" + key.nume + "</a></li>";
            });
            output += "</ul>";
            $("#result").show( "slow", function(){
                $(this).html(output)
            });
        }
    });
});
 $("#result").on("click", "a", function(e){
    e.PreventDefault;
    id = $("#damus").data("target");
    $("#json").val(id);
 });
});

DSA

Here's the testing JSON array:

 [{"id":"1","nume":"Test one"},{"id":"2","nume":"Test two"},{"id":"3","nume":"Test three"},{"id":"4","nume":"Test four"}]

What I want to do is to update an input based on clicking on AJAX-returned options, but it only update's the value with "Test" instead on "Test one" (for example).

I've searched here, but only got methods for how to trim/delete spaces.

Upvotes: 0

Views: 66

Answers (1)

user5578554
user5578554

Reputation:

Something that jumps out at me is that all of your a tags have the same id. So if you click on any of the links, the first link's data-target attribute will be displayed.

Upvotes: 1

Related Questions