0

I'm a bit of a beginner, but I have a code webpage that makes a post request to my server and replies with a JSON encoded array of associated arrays.

Next I use a text input field and on .keyup the code works with the array as a sort of search feature.

$( function() {
$('#questionSearchInput').keyup(function(){
    var list = [];
    search = $(this).val().toString();
    searchArray = search.split(" ");
    console.log(searchArray);
    $.each(grabbedItems, function(j, question){
        question.score = 0;
        console.log(question.score);
    });
    if(searchArray[0]){
        $.each(grabbedItems, function(i, question){
            lowerName = question.name.toLowerCase();
            if(lowerName.indexOf(searchArray[0]) > -1){
                question.score = question.score + 1;
                console.log(question.score);
            }
        });
    }

It runs the if(searchArray[1]) section a few times for each element in the search array and then dynamically loads onto the page.

        grabbedItems.sort(function(a, b){
        return b.score - a.score;
    });
    if(grabbedItems.length < 1){
        document.getElementById("noQuestionsP").style.display = "block";
    }
    clearBox("questionNamesDisplay");
    $.each(grabbedItems, function(k, question){
        if(k < 5){
            var place = document.getElementById("questionNamesDisplay");
            var item = document.createElement("a");
            item.innerHTML = question.name;
            item.href = question.link;
            item.style.display = "block";
            place.appendChild(item);
        }
    });
});

});

What I mean is that .keyup doesn't work on Iphones as the input element must be dealt with differently.

Does anyone with more knowledge than me know of a better solution or way of fixing this?

Tom Albanese
  • 173
  • 1
  • 11
  • 1
    use on change on the input. Keep it simple stupid :P – r3wt Oct 28 '15 at 19:07
  • I did think of that, but thought there might have been a clever solution, do Iphones not work well with jquery – Tom Albanese Oct 28 '15 at 19:13
  • is this jquery mobile ? – trk Oct 28 '15 at 19:22
  • There is some discussion here: http://stackoverflow.com/questions/9940829/how-to-capture-the-onscreen-keyboard-keydown-and-keyup-events-for-touch-devi that might help ? (you are probably already taking a look at it ... ) – trk Oct 28 '15 at 19:26

1 Answers1

3

The touchend event is fired when a touch point is removed from the device.

https://developer.mozilla.org/en-US/docs/Web/Events/touchend

You can pass keyup and touchend events into the .on() jQuery method (instead of the keyup() method) to trigger your code on both of these events.

$('#questionSearchInput').on('keyup touchend', function(){
var list = [];
search = $(this).val().toString();
searchArray = search.split(" ");
console.log(searchArray);
$.each(grabbedItems, function(j, question){
    question.score = 0;
    console.log(question.score);
});
if(searchArray[0]){
    $.each(grabbedItems, function(i, question){
        lowerName = question.name.toLowerCase();
        if(lowerName.indexOf(searchArray[0]) > -1){
            question.score = question.score + 1;
            console.log(question.score);
        }
    });
}
Antfish
  • 1,313
  • 2
  • 22
  • 41