0

ajax i have this snippet:

$("input#search_field").keyup(function(){
        var searchText = $("input#search_field").val()
        if(searchText.length > 1){
            $.ajax({
                url: 'search.php',
                data: {data: JSON.stringify(searchText)},
                type: 'POST',
                dataType: "json",
                success: function (data) {
                    if(data.result == 1) {
                        console.log(data.error);
                    }
                    if(data.result == 0) {
                        console.log(data.error)
                    }
                }
            });
        } 
});

When data.result is = 1, than the returned data.error is an array, in my console:

["string"]

My question is how get every string in my array into a different variable so i can use it later?

Because returned array could be also:

["string","string2","string3"]

Anyone knows?? Greetings!

7
  • Do you want to save the search values only for a given session (before the browser window is closed) or forever? Apr 8, 2014 at 17:15
  • for a given session...until the event is fired again :) you know? Apr 8, 2014 at 17:16
  • 5
    They are already in three separate locations. data.result[0], data.result[1], and data.result[2]. Why do you need to move it to separate variables if you can already access them as-is?
    – Kevin B
    Apr 8, 2014 at 17:16
  • can i apply data.result[0] etc to html later? Apr 8, 2014 at 17:18
  • 1
    @user3297073 you can use .length to get the number of elements in the array (e.g. array.length). Apr 8, 2014 at 17:39

1 Answer 1

0

I split the array as folow

var string = $('#uInput').val();
var array = string.split('\n');
for($i = 0; $i < array.length; $i++){
    var dateAndText = array[$i].split(',');
    $('#spota').append(dateAndText[0] + '<br>');
    $('#spotb').append(dateAndText[1] + '<br>');

Hope this helps.

1
  • Don't see how this is related to the question. Mar 27, 2017 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.