0

I am currently working on adding an array to another array by writing something in a texfield and clicking on a button. I also have an if statement, that is supposed to check if the strings im writing already exist in the table, and if it does, i should get an alert. However, i get an alert every time i try to add something, even though my array is empty. Any solution?

Javascript

$(document).ready(function(){

var films = [];

// ändra bakgrundsfärg för textfield 
$('#name').keyup(function(){
            $('#name').css('background-color', 'white');
});

// ändra bakgrundsfärg för select lista
$('#options').change(function(){
            $('#options').css('background-color', 'white');
});

// kör funktionen när knappen med id "button" klickas
$("#button").click(function(){
    var titelBetyg = [];

    var titel = $('#name').val();
    var betyg = $('#options').val();

    // kontrollera om textfield är tom
    if(titel == ""){
        $('#name').css('background-color', 'red');
        alert("Mata in titel.....");
    }

    // kontrollera om select lista inte är vald
    else if(betyg == "0"){
        $('#options').css('background-color', 'red');
        alert("Välj betyg....");
    }

         // HERE IS THE IF STATEMENT WHERE IT FAILS
    // kontrollera om filmen redan finns
    else if($.inArray($("#name"), films) > -1){
        alert("Filmen finns redan...");
    }

    else{
        titelBetyg.push(betyg);
        titelBetyg.push(titel);
        films.push(titelBetyg);

        var ul = $('#rightbar ul').empty();
        if (!ul.length){ // om ul inte finns
            ul = $("<ul>").appendTo('#rightbar'); // skapa en ny
        }

        // loopa igenom arrayen och placera innehållet i ul och li
        for (var i=0; i<films.length; i++){
            ul.append("<li>" + films[i][1] + " " + "<span>" + films[i][0] + "</span>" + "</li>");
        }

        $('#form').get(0).reset();

    }
});

// kör funktionen när knappen med id "stigande" klickas
 $('#stigande').click(function () {
    if (films.length > 1) {
        films.sort(function (a, b) {
            return (a[0].length < b[0].length) ? 1 : -1;
        })
        var ul = $('#rightbar ul').empty();
        for (var i = 0; i < films.length; i++) {
            ul.append("<li>" + films[i][1] + " " + "<span>" + films[i][0] + "</span>" + "</li>");
        }
    }
});

// kör funktionen när knappen med id "fallande" klickas
$('#fallande').click(function () {
    if (films.length > 1) {
        films.sort(function (a, b) {
            return (a[0].length > b[0].length) ? 1 : -1;
        })
        var ul = $('#rightbar ul').empty();
        for (var i = 0; i < films.length; i++) {
            ul.append("<li>" + films[i][1] + " " + "<span>" + films[i][0] + "</span>" + "</li>");
        }
    }
});

});

2 Answers 2

2
$.inArray($("#name"), films) > -1

You're checking if a collection of jQuery elements exists in an array, you probably meant:

$.inArray($.trim( $("#name").val() ), films) > -1
Sign up to request clarification or add additional context in comments.

4 Comments

didnt help, the if statement still gave me the alert.
else if(!$.inArray($.trim($("#name").val()), films) > -1){ alert("Filmen finns redan..."); } yet i still got the alert
console.log the value and the array and see what you've got. And if using not, do != -1 instead of placing the not at the start of the condition.
I placed the not as you said, != -1, but now it doesnt care if the value exists, it adds the new value either way.
1

Is $('#name') an input? If so, you should use $('#name').val() instead of just $('#name').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.